MVC 4 Ajax.BeginForm POST不绑定到控制器中的模型

时间:2014-01-17 16:47:22

标签: ajax asp.net-mvc asp.net-mvc-4 asp.net-ajax

对不起这里的代码数量,但这是解释内容的最佳方式。

我在MVC 4局部视图中提供了此代码:

  <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
  @using (Ajax.BeginForm("TestPost", new AjaxOptions { HttpMethod = "Post" }))
    {                               
        foreach (var d in Model.DataItemsWithLabels)
        {                                    
            @Html.LabelFor(m => d.DataName)
            @Html.TextBoxFor(m => d.DataValue);             
        }

        <input type="submit" value="Save" />
    }

我的控制器操作如下:

 public ActionResult TestPost(CmaPartialModel model)
 {         
        return PartialView("Transaction", model);
 }

在我的模型中,我有一些代码填充了一个对象列表(只有它们是空的),定义为:

 public List<DataItemWithLabel> DataItemsWithLabels { get; set; }      

    public class DataItemWithLabel
    {
        public string DisplayName { get; set; }

        public string DataName { get; set; }

        [Required]
        public string DataValue { get; set; }
    }  

我还认为正确的web.config条目是什么:

                 

我在这里和其他地方阅读过大量帖子但没有成功。

代码会发布到TestPost方法,但模型始终为空。有人可以告诉我为什么会这样吗?

3 个答案:

答案 0 :(得分:7)

您始终获得null值的原因是模型绑定器不知道如何绑定对象列表。见下面的例子:

public class CmaPartialModel 
{
    public List<DataItemWithLabel> DataItemsWithLabels { get; set; } 
}

然后在你看来使用它:

@model  CmaPartialModel 

<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
@using (Ajax.BeginForm("TestPost", new AjaxOptions { HttpMethod = "Post" }))
{
    for(var i = 0; i < Model.DataItemsWithLabels.Count; i++)
     {
        @Html.LabelFor(m => m.DataItemsWithLabels[i].DataName)
        @Html.TextBoxFor(m => m.DataItemsWithLabels[i].DataValue)
     }
    <input type="submit" value="Save" />
}

最后,您的操作方法应如下所示:

 public ActionResult TestPost(CmaPartialModel model)
 {         
        return PartialView("Transaction", model);
 }

更新

您也可以在视图中使用foreach,如下所示:

foreach (var item in Model.DataItemsWithLabels.Select((value, i) => new { i, value }))
{
   @Html.LabelFor(m => m.DataItemsWithLabels[@item.i].DataName)
   @Html.TextBoxFor(m => m.DataItemsWithLabels[@item.i].DataValue);   
}

答案 1 :(得分:1)

I think below code will help you.

查看

@model MvcApplication1.Models.CmaPartialModel   
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Ajax.BeginForm("TestPost", new AjaxOptions { HttpMethod = "Post" }))
 {
   if (Model != null)
    {
    foreach (var d in Model.DataItemsWithLabels)
    {                                    
        @Html.LabelFor(m => d.DataName)
        @Html.TextBoxFor(m => d.DataValue);
    }
   }
  <input type="submit" value="Save" />
}

<强>模型

public class CmaPartialModel
{
    public List<DataItemWithLabel> DataItemsWithLabels { get; set; }

    public class DataItemWithLabel
    {
        public string DisplayName { get; set; }

        public string DataName { get; set; }

        public string DataValue { get; set; }
    }  
}

<强>控制器

public ActionResult Index()
    {
        return View();
    }
    public ActionResult TestPost(CmaPartialModel model)
    {
        List<CmaPartialModel.DataItemWithLabel> parts = new      List<CmaPartialModel.DataItemWithLabel>();
        parts.Add(new CmaPartialModel.DataItemWithLabel() {DisplayName = "abc",DataName = "cbv",DataValue = "343"});
        parts.Add(new CmaPartialModel.DataItemWithLabel() { DisplayName = "sfda", DataName = "xzfdsf", DataValue = "234" });
        model.DataItemsWithLabels = parts;
        return PartialView("Index", model);
    }

答案 2 :(得分:0)

您需要修改Post方法,如下所示

public ActionResult TestPost(IEnumerable<DataItemWithLabel> model)
 {         
        return PartialView("Transaction", model);
 }

由于您将DataItemWithLabel模型的集合传递给控制器​​。