2013-11-25 66 views
13

我无法绑定包含对象列表的模型。当我尝试将数据从控制器传递到视图时没有任何问题,但是当我想要将数据发回时,我收到消息说该方法不存在。MVC模型绑定对象列表

我正在使用ajax调用,并将数据放在$ form.serialize()中,并可以看到列表中的所有数据,但我没有绑定的运气。

的型号是:

public class Single 
{ 
    public int Id {get;set;} 
    public string Name {get;set;} 
    public List<SimpleDropdown> dddl {get;set;} 
    public int SelectedEmp {get;set;} 
} 
public class MainModel 
{ 
    public List<Single> main_model_list {get;set;} 
} 

在我的控制器的方法现在是:

[HttpPost] 
public string SaveModel(MainModel model) 
{ 
    return ""; 
} 

这种方法不会被调用,但是当我删除参数调用工作。所以我确定绑定不起作用。我有更复杂的模型,但我尽可能简化了它,但仍然无法实现。

所以我的问题是我如何测试这看看有什么问题?

编辑:

我没有此刻的代码,但是代码的功能,因为我在项目的其他地方使用它。它是这样的:

$("#form").submit(function() { 
     $.ajax({ 
     url: "/Controller/SaveModel", 
     type: "POST", 
     data: $(this).serialize() 
    }); 
}); 

的形式看起来是这样的:

@using (Html.BeginForm("SaveModel", "Home", FormMethod.Post, new { id = "form" })) 
{ 
    @for (var z = 0; z < ViewBag.groupes.Length; z++) 
    { 
     <div style="border-left: 1px solid black"> 
      <h1>@ViewBag.groupes[z]</h1> 
     </div> 
    } 
    @for (var i = 0; i < Model.main_model_list.Count; i++) 
    { 
     <div>@Html.LabelFor(x => x.main_model_list[i].Id)</div> 
     <div>@Html.LabelFor(x => x.main_model_list[i].Name)</div> 
     <div style="float: left">@Html.DropDownListFor(x => main_model_list[i].SelectedEmp, new SelectList(main_model_list[i].dddl, "Id", "Value", main_model_list[i].SelectedEmp), new { @class = "preferences_dd_lists" })</div> 
    } 
} 
+3

你的ajax调用在哪里? – PSL

+0

你如何输出视图?你是否偶然使用“foreach”? –

+0

我使用for语句传递main_model_list列表中的所有元素及其属性。 – Aleks

回答

1

尝试以下操作:

$("#form").submit(function() { 
     $.ajax({ 
     url: "/Controller/SaveModel", 
     contentType: "application/json", 
     type: "POST", 
     data: $(this).serialize() 
    }); 
}); 
+0

我仍然有同样的问题。 – Aleks

1

而结合的粘合剂寻找值Id这是不由您提供。 所以在循环中您的视图页面添加此

@Html.HiddenFor(x => x.main_model_list[i].Id) 
1

我敢肯定,你写HTML错误,你要收集发布到控制器,但您的数据形式不与模型相匹配。

我认为,如果你想收集发送到控制器,你必须修改你的HTML格式的 这样的:

<input type="hidden" name="main_model_list[0].Id" value="1 /> 
<input type="hidden" name="main_model_list[0].Name" value="xyz" /> 
<select name="main_model_list[0].SelectedEmp"> 
..... 
</select> 
<input type="hidden" name="main_model_list[1].Id" value="1 /> 
<input type="hidden" name="main_model_list[1].Name" value="xyz" /> 
<select name="main_model_list[1].SelectedEmp> 
..... 
</select> 

............... ..

我想你应该从单一模型中删除List dddl。

1

我已经执行了下面的代码,它工作正常。声明模型(S)如下

public class SimpleDropdown 
{ 
    // 
    // Summary: 
    //  Gets or sets the text of the selected item. 
    // 
    // Returns: 
    //  The text. 
    public string Text { get; set; } 
    // 
    // Summary: 
    //  Gets or sets the value of the selected item. 
    // 
    // Returns: 
    //  The value. 
    public string Value { get; set; } 
} 

public class SingleEntity 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public List<SimpleDropdown> dddl { get; set; } 
    public int SelectedEmp { get; set; } 
} 

public class MainModel 
{ 
    public List<SingleEntity> main_model_list { get; set; } 
} 

和控制器的动作方法如下

public class BasicController : Controller 
{ 
    public ActionResult GetModel() 
    { 
     MainModel mainModel = new MainModel(); 
     List<SimpleDropdown> dropdownlist = new List<SimpleDropdown>(); 
     List<SingleEntity> selist = new List<SingleEntity>(); 
     for (int j = 1; j < 10; j++) 
     { 
      dropdownlist.Add(new SimpleDropdown { Text = "Text" + j, Value = j.ToString() }); 

     } 
     SingleEntity se; 
     for (int i = 0; i < 10; i++) 
     { 
      se = new SingleEntity(); 
      se.Id = i; 
      se.Name = "Name" + i; 
      se.SelectedEmp = i; 
      se.dddl = dropdownlist; 
      mainModel.main_model_list = selist; 
      mainModel.main_model_list.Add(se); 

     } 
     return View(mainModel); 
    } 

    [HttpPost] 
    public ActionResult SaveModel(MainModel mainModelasdfafsf) 
    { 
     // do stuff here... please not only selectedEmp only will get reflected but not the property ddl since its again another list 
     return View("GetModel"); 
    } 
} 

终于视图应该是这样的

@model MvcDemo.Models.MainModel 
@using (Html.BeginForm("SaveModel", "Basic", FormMethod.Post, new { id = "formModel" })) 

{

for (var i = 0; i < Model.main_model_list.Count; i++) 
{ 
<div>@Html.TextBoxFor(x => Model.main_model_list[i].Id)</div> 
<div>@Html.TextBoxFor(x => Model.main_model_list[i].Name)</div> 
<div> 
    @Html.TextBoxFor(x => Model.main_model_list[i].SelectedEmp) 
</div> 
<div style="float: left"> 
    @Html.DropDownListFor(x => Model.main_model_list[i].SelectedEmp, new SelectList(Model.main_model_list[i].dddl, "Text", "Value", Model.main_model_list[i].SelectedEmp), new { @class = "preferences_dd_lists" }) 

</div> 

} 

<input type="button" value="Save" id="btnsave" name="btnsave" /> 

}

@section Scripts{ 
<script type="text/javascript"> 
    $(document).ready(function() { 
     $('#btnsave').click(function() { 
      $.ajax({ 
       async: false, 
       cache: false, 
       data: $('#formModel').serialize(), 
       url: '@Url.Action("SaveModel", "Basic")', 
       type: "POST", 
       dataType: "text/html", 
       success: function() { 
        alert('success'); 
       }, 
       error: function (errorThrown) { 
        debugger; 
        alert('something went wrong!!!'); 
       } 

      }); 
     }); 
    }); 
</script> 

}

高于一切确保所有脚本文件被加载,也 Check this Link where Model binding will not work for Lable and LableFor

2

你可以尝试使用

@using(Ajax.BeginForm("SaveModel", "controller", formMethod.Post, new AjaxOptions{ })) 
    { 
     // your form 
    } 

不要忘记使用jquery.unobtrusive-ajax.js

与Ajax.BeginForm您可以删除的jQuery的代码块发送您的形式,它使你的形式绑定直接在Action中。

2

您可以更改您的代码。

data:{ model:$(this).serialize()} 

因为您不提供参数名称,mvc模型绑定不起作用。