MVC 4 Ajax.BeginForm POST不绑定到控制器中的模型 [英] MVC 4 Ajax.BeginForm POST does not bind to model in conroller

查看:100
本文介绍了MVC 4 Ajax.BeginForm POST不绑定到控制器中的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,这里的代码数量很多,但这是解释正在发生的事情的最佳方法.

Sorry for the amount of code here but it's the best way to explain what;s happening.

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

I gave this code in my MVC 4 partial view:

  <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);
 }

在我的模型中,我有一些代码可填充定义为以下内容的对象列表(仅当它们为空时):

In my model I have some code that populates a list of objects (only if they are empty) that are defined as:

 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条目是

I also have what think is the correct web.config entries:

我在这里和其他地方读了很多帖子,都没有成功.

I have read a load of posts here and elsewhere with no success.

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

The code does post to the TestPost method but the model is always empty. Can someone please tell me why this might be?

推荐答案

总是获得null值的原因是模型绑定程序不知道如何绑定对象列表.参见以下示例:

The reason you always get null value is the model binder doesn't know how to bind the list of Objects. See below example:

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

然后在您的视图中使用它:

Then use it in you view:

@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" />
}

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

Finally, your action method should be like below:

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

更新

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

You also can use foreach in your view like below:

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);   
}

这篇关于MVC 4 Ajax.BeginForm POST不绑定到控制器中的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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