asp.net-mvc HTML.HiddenFor 值集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12249171/
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.HiddenFor value set
提问by arnoldrob
@Html.HiddenFor(model => model.title, new { id= "natureOfVisitField", @value = '@Model.title'})
it doesen't work! how to set the value?
它不起作用!如何设置值?
采纳答案by Daniel A. White
You shouldn't need to set the value in the attributes parameter. MVC should automatically bind it for you.
您不需要在属性参数中设置值。MVC 应该自动为您绑定它。
@Html.HiddenFor(model => model.title, new { id= "natureOfVisitField" })
回答by user1187093
For setting value in hidden field do in the following way:
要在隐藏字段中设置值,请按以下方式进行:
@Html.HiddenFor(model => model.title,
new { id= "natureOfVisitField", Value = @Model.title})
It will work
它会工作
回答by Tejasvi Hegde
Strange but, Try with @Value, capital "V"
奇怪,但是,尝试@Value,资本"V"
e.g. (working on MVC4)
例如(在 MVC4 上工作)
@Html.HiddenFor(m => m.Id, new { @Value = Model.Id })
Update:
更新:
Found that @Value (capital V) is creating another attribute with "Value" along with "value", using small @value seems to be working too!
发现@Value(大写 V)正在创建另一个带有“Value”和“value”的属性,使用小的 @value 似乎也有效!
Need to check the MVC source code to find more.
需要检查MVC源代码以找到更多。
Update, After going through how it works internally:
更新,经过内部工作原理后:
First of all forget all these workarounds (I have kept in for the sake of continuity here), now looks silly :)
首先忘记所有这些解决方法(为了保持连续性,我保留在这里),现在看起来很傻:)
Basically, it happens when a model is posted and the model is returned back to same page.
基本上,它发生在发布模型并将模型返回到同一页面时。
The value is accessed (and formed into html) in InputHelper method (InputExtensions.cs) using following code fragment
使用以下代码片段在 InputHelper 方法 (InputExtensions.cs) 中访问该值(并形成为 html)
string attemptedValue = (string)htmlHelper.GetModelStateValue(fullName, typeof(string));
The GetModelStateValue method (in Htmlelper.cs) retrieves the value as
GetModelStateValue 方法(在 Htmlelper.cs 中)将值检索为
ViewData.ModelState.TryGetValue(key, out modelState)
Here is the issue, since the value is accessed from ViewData.ModelStatedictionary.
This returns the value posted from the page instead of modified value!!
这是问题所在,因为该值是从ViewData.ModelState字典中访问的。这将返回从页面发布的值而不是修改后的值!!
i.e. If your posted value of the variable (e.g. Person.Id) is 0 but you set the value inside httpPost action (e.g. Person.Id = 2), the ModelState still retains the old value "0" and the attemptedValuecontains "0" ! so the field in rendered page will contain "0" as value!!
即如果您发布的变量值(例如 Person.Id)为 0,但您在 httpPost 操作中设置了该值(例如 Person.Id = 2),则 ModelState 仍保留旧值“0”,而尝试值包含“0” !所以渲染页面中的字段将包含“0”作为值!!
Workaround if you are returning model to same page : Clear the item from ModelState,
如果您将模型返回到同一页面,解决方法:从 ModelState 中清除该项目,
e.g.
例如
ModelState.Remove("Id");
This will remove the item from dictionary and the ViewData.ModelState.TryGetValue(key, out modelState) returns null, and the next statement (inside InputExtensions.cs) takes the actual value (valueParameter) passed to HiddenFor(m => m.Id)
这将从字典中删除项目, ViewData.ModelState.TryGetValue(key, out modelState) 返回 null,下一个语句(在 InputExtensions.cs 中)采用传递给 HiddenFor(m => m.Id ) 的实际值(valueParameter) )
this is done in the following line in InputExtensions.cs
这是在 InputExtensions.cs 中的以下行中完成的
tagBuilder.MergeAttribute("value", attemptedValue ?? ((useViewData) ? htmlHelper.EvalString(fullName, format) : valueParameter), isExplicitValue);
Summary:
概括:
Clear the item in ModelState using:
使用以下命令清除 ModelState 中的项目:
ModelState.Remove("...");
Hope this is helpful.
希望这是有帮助的。
回答by der_chirurg
It is just Value, not @value.. Try it. I'm not sure about @Model.title, maybe it's just Model.title
它只是 Value,而不是 @value.. 试试看。我不确定 @Model.title,也许它只是 Model.title
回答by Stevoman
Necroing this question because I recently ran into the problem myself, when trying to add a related property to an existing entity. I just ended up making a nice extension method:
解决这个问题是因为我最近在尝试向现有实体添加相关属性时遇到了这个问题。我刚刚完成了一个很好的扩展方法:
public static MvcHtmlString HiddenFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, TProperty value)
{
string expressionText = ExpressionHelper.GetExpressionText(expression);
string propertyName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(expressionText);
return htmlHelper.Hidden(propertyName, value);
}
Use like so:
像这样使用:
@Html.HiddenFor(m => m.RELATED_ID, Related.Id)
Note that this has a similar signature to the built-in HiddenFor, but uses generic typing, so if Value is of type System.Object, you'll actually be invoking the one built into the framework. Not sure why you'd be editing a property of type System.Object in your views though...
请注意,这与内置的 HiddenFor 具有相似的签名,但使用通用类型,因此如果 Value 是 System.Object 类型,您实际上将调用框架中内置的那个。不确定为什么要在视图中编辑 System.Object 类型的属性...
回答by Mike
The @ symbol when specifying HtmlAttributes is used when the "thing" you are trying to set is a keyword c#. So for instance the word class, you cannot specify class, you must use @class.
当您尝试设置的“事物”是关键字 c# 时,使用指定 HtmlAttributes 时的 @ 符号。所以例如类这个词,你不能指定类,你必须使用@class。
回答by Ali Hasan
You can do this way
你可以这样做
@Html.HiddenFor(model=>model.title, new {ng_init = string.Format("model.title='{0}'", Model.title) })

