asp.net-mvc MVC @Html.Display()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7968395/
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
MVC @Html.Display()
提问by Nate Pet
I have something like:
我有类似的东西:
<input type="text" name="TerrMng" id="TerrMng"/>
in HTML. What is the equivalent of the above using @Html.Display?
在 HTML 中。上面使用@Html.Display 的等价物是什么?
I tried using: @Html.Display("TerrMng", TerrMng)
我尝试使用: @Html.Display("TerrMng", TerrMng)
but was not successful. Note that I like to use @Html.Display but not sure how to translate the ID value so that it shows up.
但没有成功。请注意,我喜欢使用 @Html.Display 但不确定如何转换 ID 值以使其显示。
采纳答案by Josh Mein
This should do the trick if you are just wanting to display the data and not allow the user to edit the information.
如果您只想显示数据并且不允许用户编辑信息,这应该可以解决问题。
@Html.DisplayFor(m => m.TerrMng);
Edit:
编辑:
what-is-the-html-displayfor-syntax-foris another question on stackoverflow that may give you some more guidance.
what-the-html-displayfor-syntax-for是关于 stackoverflow 的另一个问题,它可以为您提供更多指导。
Edit:
编辑:
TerrMng does not exist on PageLoad so you cannot use the Html.Displayin that way. You need to create it and fill its value with the value received from the jQuery. In this case where you would have to do the following:
PageLoad 上不存在 TerrMng,因此您不能以这种方式使用Html.Display。您需要创建它并用从 jQuery 接收到的值填充它的值。在这种情况下,您必须执行以下操作:
HTML
HTML
@Html.Display("TerrMng"); // This creates the label with an id of TerrMng
jQuery
jQuery
$("#TerrMng").val(TerrMng); // This puts the value of the javascript variable into the label
回答by Patrick Karcher
The Displaymethod is not for creating input boxes. You'd want to use:
该Display方法不是用于创建输入框。你想使用:
@Html.TextBoxFor(m => m.TerrMng);
or the templated helpermethod:
或模板化的辅助方法:
@Html.EditorFor(m => m.TerrMng);
I'm assuming that you want to use modelbinding. If not, if you really just want to use a helper to simply make an input tag, use:
我假设您想使用模型绑定。如果没有,如果您真的只想使用助手来简单地制作输入标签,请使用:
@Html.TextBox("TerrMng");
This would be sent to the client:
这将发送给客户端:
<input id="TerrMng" type="text" value="" name="TerrMng">
The first 2 methods above would result in the exact same html, if model.TerrMng was "" or String.Empty. If for some reason you don't want the value attribute, you'll need to type it out yourself.
如果 model.TerrMng 是 "" 或String.Empty. 如果由于某种原因您不想要 value 属性,则需要自己输入。
回答by Hari Gillala
You could try something based on this. This is not exact but you could get some idea.
你可以尝试基于此的东西。这并不准确,但您可以有所了解。
@Html.TextBoxFor(yourmodel => model.yourModelFieldname, null)
回答by Mladen B.
@Html.Display()is used instead of @Html.DisplayFor()when your model is not known at compile time, or if you prefer to work with strings, rather than with strong types. For example, these 2 are equivalents (given that your model is some class):
@Html.Display()@Html.DisplayFor()当您的模型在编译时未知时,或者您更喜欢使用字符串而不是强类型时,将使用它来代替。例如,这两个是等价的(假设您的模型是某个类):
@Html.DisplayFor(m => m.MyProperty)
and
和
@Html.Display("MyProperty")
But the additional cool feature of the Display() method is that it can also do the lookup in the ViewData, and not just in your Model class. For example, here is a way to display the HTML for the property on a random object, given that we know it has a property named "Blah" (the type of the object doesn't really matter):
但是 Display() 方法的另一个很酷的功能是它也可以在 ViewData 中进行查找,而不仅仅是在您的 Model 类中。例如,这是一种在随机对象上显示属性的 HTML 的方法,假设我们知道它有一个名为“Blah”的属性(对象的类型并不重要):
@{ ViewData["itsawonderfullife"] = SomeObject; }
<div>@Html.Display("itsawonderfullife.Blah")</div>
This way, we are telling HtmlHelperto look into the ViewData, instead of our Model, and to display the property Blahof a given SomeObject.
通过这种方式,我们告诉我们HtmlHelper要查看ViewData,而不是我们的Model,并显示Blah给定的属性SomeObject。

