asp.net-mvc MVC4 Razor - @Html.DisplayFor 未绑定到模型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11056954/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:02:13  来源:igfitidea点击:

MVC4 Razor - @Html.DisplayFor not binding to model

asp.net-mvcrazor

提问by Jacques

I am trying to find me feet with MVC4 Razor and I'm stuck with this simple problem.

我试图用 MVC4 Razor 找到我的脚,但我被这个简单的问题困住了。

When I use @Html.DisplayFor the model is always sent back as NULL, but when I use @Html.TextBoxFor this model is fully populated, what am I missing?

当我使用 @Html.DisplayFor 时,模型总是作为 NULL 返回,但是当我使用 @Html.TextBoxFor 时,这个模型是完全填充的,我错过了什么?

Thanks in advance

提前致谢

回答by Travis J

This is a common issue that many people miss in the asp.net mvc framework. Not just the difference in the helpers such as HiddenFor, DisplayFor, TextBoxFor- but how exactly the framework sets up automatically collecting and validating these inputs. The magic is all done with HTML5's data-* attributes. You will notice when looking at the input tag generated that there are going to be some extra properties in the form of data-val, data-val-required, and perhaps some additional data properties for types, for example numerics would be data-val-number.

这是很多人在 asp.net mvc 框架中遗漏的常见问题。不仅仅是像HiddenFor, DisplayFor, TextBoxFor-这样的帮助器的不同,还有框架如何设置自动收集和验证这些输入。神奇的一切都是通过 HTML5 的 data-* 属性完成的。在查看生成的输入标记时,您会注意到将有一些额外的属性,形式为data-val, data-val-required,也许还有一些类型的额外数据属性,例如数字是data-val-number

These data attributes allow the jQuery extension jquery.validate.unobtrusive.jsto parse the DOM and then decide which fields to validate or generate error messages.

这些数据属性允许 jQuery 扩展jquery.validate.unobtrusive.js解析 DOM,然后决定验证哪些字段或生成错误消息。

The actual collection of posted data is reflected from the nameproperty. This is what should map up to the model that is in the c# or vb [HttpPost]method.

已发布数据的实​​际集合从name属性中反映出来。这是应该映射到 c# 或 vb[HttpPost]方法中的模型的内容。

Use HiddenForwhen you want to provide posted data that the user does not need to be aware of.

HiddenFor当您想提供用户不需要知道的发布数据时使用。

Use DisplayForwhen you want to show records but not allow them to be editted.

使用DisplayFor时要显示的记录,但不允许他们被editted。

Use TextBoxForwhen you want to allow user input or allow the user to edit a field.

使用TextBoxFor时,要允许用户输入或允许用户编辑的字段。

EDIT

编辑

"the purpose of this view is to enable the user to view the data before submitting it to the database. Any ideas how I can achieve this?"

"the purpose of this view is to enable the user to view the data before submitting it to the database. Any ideas how I can achieve this?"

You could accomplish this with a duo of HiddenForand DisplayFor. Use HiddenForto have the values ready to be posted, and DisplayForto show those values.

你可以用二重奏做到这一点HiddenForDisplayFor。用于HiddenFor准备发布值,并DisplayFor显示这些值。

回答by Shyju

DisplayForwill not do the Model binding. TextBoxForwill do because it creates a inputelement in the form and the form can handle it when it is being posted. If you want to get some data in the HttpPost action and you dont want to use the TextBoxFor, you can keep that pirticulare model proeprty in a hidden variable inside the form using the HiddenForHTML helper method like this.

DisplayFor不会做模型绑定。TextBoxFor会这样做,因为它会input在表单中创建一个元素,并且表单可以在发布时处理它。如果您想在 HttpPost 操作中获取一些数据并且不想使用 TextBoxFor,则可以使用HiddenFor像这样的HTML 帮助程序方法将该 pirticulare 模型属性保存在表单内的隐藏变量中。

@using(Html.BeginForm())
{
  <p>The Type Name is</p> @Html.DisplayFor(x=>x.TypeName)
  @Html.HiddenFor(x=>x.TypeName)
  <input type="submit" value="Save" />
}

回答by The Turbinator

Use both DisplayForand HiddenFor. DisplayForsimply displays the text and is not an input field, thus, it is not posted back. HiddenForactually creates <input type="hidden" value="xxxx"/>

使用DisplayForHiddenForDisplayFor只是显示文本而不是输入字段,因此不会回传。 HiddenFor实际上创造<input type="hidden" value="xxxx"/>

回答by Justin Helgerson

DisplayForbuilds out a HTML label, not an input. Labels are not POSTed to the server, but inputs are.

DisplayFor构建一个 HTML 标签,而不是一个输入。标签不会发布到服务器,但输入会。

回答by Tommy

Do you mean during a form post? If you use DisplayFor, this creates a element which does not contain any form values. Typically you use these in conjunction with each other to create a label for your textbox, then using the Html.TextBoxFor to allow users to modify the data element.

你的意思是在表格发布期间?如果您使用 DisplayFor,这将创建一个不包含任何表单值的元素。通常,您将它们相互结合使用来为文本框创建标签,然后使用 Html.TextBoxFor 来允许用户修改数据元素。

Example:

例子:

@Html.DisplayFor(x=>x.Item)
@Html.TextBoxFor(x=>x.Item)

Will Render

将呈现

Item <a text input field following>

Or in HTML

或者在 HTML 中

<label for="Item">Item</label><input type="Text" id="Item" name="Item"/>

回答by pixelda

I know this is a bit of an old question but you can roll your own, custom combined display control as shown below. This renders the model value followed by a hidden field for that value

我知道这是一个老问题,但您可以推出自己的自定义组合显示控件,如下所示。这将呈现模型值,后跟该值的隐藏字段

@Html.DisplayExFor(model => Model.ItemCode)

Simply use what the framework already has in place

只需使用框架已有的东西

public static MvcHtmlString DisplayExFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> ex)
{
    var sb = new StringBuilder();
    sb.Append(htmlHelper.DisplayFor(ex));
    sb.Append(htmlHelper.HiddenFor(ex));

    return MvcHtmlString.Create(sb.ToString());
}