How to get HTML attribute values after postback?
-
12-12-2019 - |
Question
this is my view:
@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" />
}
this is the 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>
after form postback, how do i access to data-tmp
values in the controller?
[HttpPost]
public ActionResult Index(string test)
{
}
Request["txtName1"]
returns the valid
of textbox but not the data-tmp
attribute value. Since this can be done in asp.net web form, i assume the same in asp.net mvc also, or not?
Solution
Attributes aren't included in the Request.Forms dictionary. You would have to post your additional data as a hidden field or to the query string
OTHER TIPS
Alternatively you could store your data-temp in an Array or Dictionary and save it in a 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;
...and then use the following line to retrieve the Dictionary from the session var:
Dictionary<string, string> recoverDict = (Dictionary<string, string>)this.Session["data-tmp"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow