MVC获得一个职位的ActionResult表单控件数据 - 属性值

问题描述:

我试图重新东西在我的WinForms天没回来的HTML数据 - MVC属性。

i'm trying to recreate something i did back in winforms days with html data- attributes in mvc.

您可以在一个表单控件使用此设置一个属性:

you could set an attribute on a form control using this:

txtTest.Attributes.Add("data-myattribute", "my value");

,然后读回曾经的形式一​​直在使用贴:

and then read it back once the form had been posted using:

txtTest.Attributes["data-myattribute"]

在加入MVC的属性是一件轻而易举的:

adding the attributes in mvc is a breeze:

@Html.TextBoxFor(m => m.FirstName, new { data_myattribute = "my value" })

只是想不通,一旦形式已经发布了如何在动作结果读?!

just can't figure out how to read them in the action result once the form has been posted?!

一直在寻找周围,虽然我可以找到关于如何设置数据 - 属性值和在JavaScript中阅读loadsa帖子,我找不到任何东西,会告诉我如何让他们回到code。 ..

been searching around and whilst i can find loadsa posts on how to set data- attribute values and read them in javascript, i can't find anything that'll tell me how to get them back in the code...

有任何人知道这个神奇的答案?!

anyone out there know the magic answer?!

数据属性不包括在该公司发布的形式,所以没有办法在你的控制器动作来读取其中的数据。尝试使用一个隐藏字段来代替:

Data attributes are not included in the data that's posted with the form, so there is no way to read them in your controller action. Try using a hidden field instead:

<input type="hidden" name="FirstNameAttribute" value="my value" />

这会绑定回模型属性:

public string FirstNameAttribute { get; set; }