【发布时间】:2012-05-30 13:30:39
【问题描述】:
我在使用 ASP.NET MVC 并将数据从视图传递到控制器时遇到问题。我有一个这样的模型:
public class InputModel {
public List<Process> axProc { get; set; }
public string ToJson() {
return new JavaScriptSerializer().Serialize(this);
}
}
public class Process {
public string name { get; set; }
public string value { get; set; }
}
我在我的控制器中创建这个 InputModel 并将它传递给视图:
public ActionResult Input() {
if (Session["InputModel"] == null)
Session["InputModel"] = loadInputModel();
return View(Session["InputModel"]);
}
在我的 Input.cshtml 文件中,我有一些代码来生成输入表单:
@model PROJ.Models.InputModel
@using(Html.BeginForm()) {
foreach(PROJ.Models.Process p in Model.axProc){
<input type="text" />
@* @Html.TextBoxFor(?? => p.value) *@
}
<input type="submit" value="SEND" />
}
现在,当我单击提交按钮时,我想处理放入文本字段中的数据。
问题 1: 我见过这个@Html.TextBoxFor(),但我并没有真正理解这个“stuff => others”。我得出的结论是,“其他东西”应该是我想要写入数据的字段,在这种情况下,它可能是“p.value”。但是箭头前面的“东西”是什么?
回到控制器,然后我有一个带有一些调试的 POST 功能:
[HttpPost]
public ActionResult Input(InputModel m) {
DEBUG(m.ToJson());
DEBUG("COUNT: " + m.axProc.Count);
return View(m);
}
这里的调试只显示如下内容:
{"axProc":[]}
COUNT: 0
所以我得到的返回模型是空的。
问题 2: 我在使用 @using(Html.BeginForm()) 时做错了什么?这不是正确的选择吗?如果是这样,我如何让我的模型充满数据回到控制器?
(这里不能使用“@model List
我希望有人可以填写我忽略的一些细节。
【问题讨论】:
-
你需要了解什么是 lambda 表达式。
-
我做到了,现在。谢谢你的名字。
标签: asp.net-mvc controller ienumerable html.beginform