保存动态元素列表的最佳方法 [英] Best way to save dynamic List of elements

查看:51
本文介绍了保存动态元素列表的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,其中包含动态元素,文本框,单选按钮,下拉列表等,现在在Asp.net中MVC我可以将模型传递给我的视图,并且在提交该页面时我拥有所有这些元素的值模特。

为此,我必须重新加载我的页面,但我不想加载我的页面,我只想用ajax / json这样做。



但我当前知道的是通过使用Ajax发布表单值我必须将所有值逐个添加到数据属性中,例如

I have a page which contains dynamic elements ,textboxes,radio buttons ,dropdownlists etc , now in Asp.net MVC i can just pass a model to my view and on submitting that page i have the values of all these elements in my model.
For this i have to reload my page , but i dont want to load my page , I just want to do this using ajax/json.

But what i currenlty know is by posting form values using Ajax i have to mark all add all values one by one to the data attribute like

$.ajax({
                        url:@Url.Action("_CreateFeature","Module")',
                        data: {
                            Id:id,
                            Name:name,
                            ...etc etc

                        },
                        dataType: "html",
                        success: function (data) {

                            $("").empty();
                            $("").append(data);
                        },





或者如果我使用Json那么我必须通过我的所有表格像这样的json格式的数据是我从一些教程得到的





or if i use Json then i have to pass all my form data to json format like this is what i got from some tutorial

var name = $("Name").val();
         var age = $("Age").val();
         var acorns = $("Acorns").val();
         var gender = $("Gender").val();
         var hobby = $("Hobby").val();
//I gues for dynamic data i have to use loop and then passing all data to dynamic 
//
//generated fields then

         // build json object
         var squirrel = {
             Name: name,
             Age: age,
             etc..etc
            
         };

         $.ajax({
             type: "POST",
             url: "home/PostSquirrel",
             traditional: true,
             contentType: 'application/json; charset=utf-8',
             data: JSON.stringify(squirrel),
             success: function (data) { console.log(data) },
             error: function (data) { console.log(data) }
         });





有没有其他方式我可以发布这个数据并且可以获得模型绑定的优势,因为在这两种方法中代码都非常脏并且dubugging变得令人头疼。



我有工作Ajax / json的经验/ Jquery所以,如果在jquery / ajax / json级别中这是不可能的,那么我也可以瘦k使用Angular js或类似的东西虽然我必须先给我时间学习这些。,plz根据我的要求建议我。



Is there any other way by which i can post this data and can get the advantage of model binding , since in both these approaches the code is very dirty and dubugging becomes an headache.

I have experience on working Ajax/json/Jquery so ,so if this is not possible in jquery/ajax/json level then i can also think to use Angular js or something similiar to that although i have to first give my time to learn these.,plz suggest me something according to my requirement.

推荐答案

。 ajax({
url:@ Url.Action( _ CreateFeature 模块'
data:{
Id:id,
Name:name,
... etc etc

},
dataType:html,
success:function(data){
.ajax({ url:@Url.Action("_CreateFeature","Module")', data: { Id:id, Name:name, ...etc etc }, dataType: "html", success: function (data) {


()。empty();
("").empty();


( ).append(数据);
},
("").append(data); },





或者如果我使用Json那么我必须通过我的所有表格像这样的json格式的数据是我从一些教程得到的





or if i use Json then i have to pass all my form data to json format like this is what i got from some tutorial

var name =


这篇关于保存动态元素列表的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆