Html.DisplayFor 未将值发布到 ASP.NET MVC 3 中的控制器 [英] Html.DisplayFor not posting values to controller in ASP.NET MVC 3

查看:17
本文介绍了Html.DisplayFor 未将值发布到 ASP.NET MVC 3 中的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Razor 中使用 ASP.NET MVC 3,下面是我的视图代码示例.

用户应该能够编辑除电子邮件地址"字段之外的所有详细信息.对于该字段,我仅使用了 Html.DisplayFor(m => m.EmailAddress).

但是当这个表单被发布时,除了 EmailAddress 之外,所有的模型属性都会被填充.

发布时如何在模型中取回电子邮件?我应该使用除 DisplayFor 以外的其他帮助程序吗?

@using (Html.BeginForm()) {@Html.ValidationSummary(true, "账户更新失败.请更正错误并重试.")<div><字段集><legend>更新账户信息</legend><div class="editor-label">@Html.LabelFor(m => m.EmailAddress)

<div class="editor-field">@Html.DisplayFor(m => m.EmailAddress)@*@Html.ValidationMessageFor(m => m.EmailAddress)*@

<div class="editor-label">@Html.LabelFor(m => m.FirstName)

<div class="editor-field">@Html.TextBoxFor(m => m.FirstName)@Html.ValidationMessageFor(m => m.FirstName)

<div class="editor-label">@Html.LabelFor(m => m.LastName)

<div class="editor-field">@Html.TextBoxFor(m => m.LastName)@Html.ValidationMessageFor(m => m.LastName)

<p><input type="submit" value="更新"/></p></fieldset>

}

请就此给我建议.

解决方案

您需要添加一个

@Html.HiddenFor(m => m.EmailAddress)

DisplayFor 不会在 POST 中发送任何内容,也不会创建输入...

顺便说一句

@Html.HiddenFor(m => m.Id)//或者任何模型键​​

有用

I am using ASP.NET MVC 3 with Razor, below is a sample from my view code.

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).

But when this form gets posted, all the model properties are filled except the EmailAddress.

How do I get the email back in the model when posting? Should I have used some helper other than 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.

解决方案

you'll need to add a

@Html.HiddenFor(m => m.EmailAddress)

DisplayFor won't send anything in POST, it won't create an input...

By the way, an

@Html.HiddenFor(m => m.Id) // or anything which is the model key

would be usefull

这篇关于Html.DisplayFor 未将值发布到 ASP.NET MVC 3 中的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆