MVC 4 Ajax.BeginForm POST 未绑定到控制器中的模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21191308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC 4 Ajax.BeginForm POST does not bind to model in conroller
提问by davy
Sorry for the amount of code here but it's the best way to explain what;s happening.
很抱歉这里的代码量很大,但这是解释正在发生的事情的最佳方式。
I gave this code in my MVC 4 partial view:
我在我的 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" />
}
And my controller action is as follows:
我的控制器操作如下:
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; }
}
I also have what think is the correct web.config entries:
我也认为正确的 web.config 条目是:
I have read a load of posts here and elsewhere with no success.
我在这里和其他地方阅读了大量帖子,但都没有成功。
The code does post to the TestPostmethod but the model is always empty. Can someone please tell me why this might be?
代码确实发布到TestPost方法,但模型始终为空。有人可以告诉我为什么会这样吗?
回答by Lin
The reason you always get nullvalue is the model binder doesn't know how to bind the list of Objects. See below example:
您总是获得null价值的原因是模型绑定器不知道如何绑定对象列表。见下面的例子:
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);
}
Update
更新
You also can use foreachin your view like below:
您还可以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);
}
回答by Pragnesh Khalas
I think below code will help you.
View
看法
@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" />
}
Model
模型
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; }
}
}
Controller
控制器
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);
}
回答by bluetoft
You need to modify your Post method as follows
你需要修改你的 Post 方法如下
public ActionResult TestPost(IEnumerable<DataItemWithLabel> model)
{
return PartialView("Transaction", model);
}
Since you're passing a collection of your DataItemWithLabelmodels to your controller.
由于您将DataItemWithLabel模型集合传递给控制器。

