如何在回发后获取HTML属性值?

时间:2012-07-27 03:09:54

标签: c# asp.net-mvc asp.net-mvc-3 html5 custom-data-attribute

这是我的观点:

@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中也是如此,或者不是吗?

2 个答案:

答案 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"];