asp.net-mvc Html.DisplayFor 未将值发布到 ASP.NET MVC 3 中的控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12314849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Html.DisplayFor not posting values to controller in ASP.NET MVC 3
提问by Yasser Shaikh
I am using ASP.NET MVC 3 with Razor, below is a sample from my view code.
我正在使用带有 Razor 的 ASP.NET MVC 3,下面是我的视图代码中的一个示例。
The user should be able to edit all their details, except the "EmailAddress" field. For that field only I have used Html.DisplayFor(m => m.EmailAddress).
用户应该能够编辑他们的所有详细信息,但“电子邮件地址”字段除外。对于那个领域,我只使用了Html.DisplayFor(m => m.EmailAddress).
But when this form gets posted, all the model properties are filled except the EmailAddress.
但是当这个表单被发布时,除了EmailAddress.
How do I get the email back in the model when posting? Should I have used some helper other than DisplayFor?
发布时如何在模型中取回电子邮件?我应该使用一些帮手DisplayFor吗?
@using (Html.BeginForm()) {
@Html.ValidationSummary(true, "Account update was unsuccessful. Please correct any errors and try again.")
<div>
<fieldset>
<legend>Update Account Information</legend>
<div class="editor-label">
@Html.LabelFor(m => m.EmailAddress)
</div>
<div class="editor-field">
@Html.DisplayFor(m => m.EmailAddress)
@*@Html.ValidationMessageFor(m => m.EmailAddress)*@
</div>
<div class="editor-label">
@Html.LabelFor(m => m.FirstName)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.FirstName)
@Html.ValidationMessageFor(m => m.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.LastName)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.LastName)
@Html.ValidationMessageFor(m => m.LastName)
</div>
<p>
<input type="submit" value="Update" />
</p>
</fieldset>
</div>
}
Please advise me on this.
请就此给我建议。
回答by Rapha?l Althaus
you'll need to add a
你需要添加一个
@Html.HiddenFor(m => m.EmailAddress)
DisplayForwon't send anything in POST, it won't create an input...
DisplayFor不会在 POST 中发送任何内容,也不会创建输入...
By the way, an
顺便说一下,一个
@Html.HiddenFor(m => m.Id) // or anything which is the model key
@Html.HiddenFor(m => m.Id) // or anything which is the model key
would be usefull
会很有用

