这是我的观点:
@using (Html.BeginForm())
{
foreach (Mvc3Intranet.Models.Dispatcher item in ViewBag.DispatcherList)
{
<div>
<input type="text" value="@(item.Fullname)" name="txtName@(item.Id)" data-tmp="@(item.Tmp)" /></div>
}
<input type="submit" name="test" value="Save" />
}
这是HTML:
<form action="/" method="post">
<div>
<input type="text" value="Ray1" name="txtName1" data-tmp="AAA" /></div>
<div>
<input type="text" value="Ray2" name="txtName2" data-tmp="BBB" /></div>
<div>
<input type="text" value="Ray3" name="txtName3" data-tmp="CCC" /></div>
<div>
<input type="text" value="Ray4" name="txtName4" data-tmp="DDD" /></div>
<input type="submit" name="test" value="Save" />
</form>
表单回发后,如何访问控制器中的data-tmp
值?
[HttpPost]
public ActionResult Index(string test)
{
}
Request["txtName1"]
返回文本框的valid
,但不返回data-tmp
属性值。既然这可以在asp.net web表单中完成,我也假设在asp.net mvc中也是如此,或者不是吗?
答案 0 :(得分:1)
Request.Forms字典中不包含属性。您必须将附加数据作为隐藏字段或查询字符串
发布答案 1 :(得分:0)
或者,您可以将数据临时存储在数组或字典中,并将其保存在session-var。
中Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("txtName1", "AAA");
dict.Add("txtName2", "BBB");
//or put the above lines in your foreach loop
this.Session["data-tmp"] = dict;
...然后使用以下行从会话var中检索词典:
Dictionary<string, string> recoverDict = (Dictionary<string, string>)this.Session["data-tmp"];