asp.net-mvc Html.HiddenFor 值属性未设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6850174/
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 property not getting set
提问by Sekhar
I could have used
我可以用
@Html.HiddenFor(x=> ViewData["crn"])
but, I get,
但是,我明白了,
<input id="ViewData_crn_" name="ViewData[crn]" type="hidden" value="500" />
<input id="ViewData_crn_" name="ViewData[crn]" type="hidden" value="500" />
To somehow circumvent that issue(id=ViewData_crn_ and name=ViewData[crn]), I tried doing the following, but the "value" attribute isn't getting set.
为了以某种方式规避该问题(id=ViewData_crn_ and name=ViewData[crn]),我尝试执行以下操作,但未设置“value”属性。
@Html.HiddenFor(x => x.CRN, new { @value="1"})
@Html.HiddenFor(x => x.CRN, new { @Value="1"})
generates
产生
<input id="CRN" name="CRN" type="hidden" value="" />
<input Value="500" id="CRN" name="CRN" type="hidden" value="" />
Am I doing anything wrong?? Thanks
我做错了什么吗??谢谢
回答by Gudradain
The following will work in MVC 4
以下将在 MVC 4 中工作
@Html.HiddenFor(x => x.CRN, new { @Value = "1" });
@Value property is case sensitive. You need a capital 'V' on @Value.
@Value 属性区分大小写。您需要在 @Value 上使用大写字母“V”。
Here is my model
这是我的模型
public int CRN { get; set; }
Here is what is output in html when you look in the browser
这是在浏览器中查看时以 html 格式输出的内容
<input value="1" data-val="true" data-val-number="The field CRN must be a number." data-val-required="The CRN field is required." id="CRN" name="CRN" type="hidden" value="1"/>
Here is my method
这是我的方法
[HttpPost]
public ActionResult MyMethod(MyViewModel viewModel)
{
int crn = viewModel.CRN;
}
回答by Darin Dimitrov
Have you tried using a view model instead of ViewData? Strongly typed helpers that end with Forand take a lambda expression cannot work with weakly typed structures such as ViewData.
您是否尝试过使用视图模型而不是 ViewData?For以 lambda 表达式结尾并采用 lambda 表达式的强类型助手不能用于弱类型结构,例如ViewData.
Personally I don't use ViewData/ViewBag. I define view models and have my controller actions pass those view models to my views.
我个人不使用 ViewData/ViewBag。我定义了视图模型并让我的控制器操作将这些视图模型传递给我的视图。
For example in your case I would define a view model:
例如在你的情况下,我会定义一个视图模型:
public class MyViewModel
{
[HiddenInput(DisplayValue = false)]
public string CRN { get; set; }
}
have my controller action populate this view model:
让我的控制器操作填充此视图模型:
public ActionResult Index()
{
var model = new MyViewModel
{
CRN = "foo bar"
};
return View(model);
}
and then have my strongly typed view simply use an EditorForhelper:
然后让我的强类型视图简单地使用一个EditorFor助手:
@model MyViewModel
@Html.EditorFor(x => x.CRN)
which would generate me:
这会产生我:
<input id="CRN" name="CRN" type="hidden" value="foo bar" />
in the resulting HTML.
在生成的 HTML 中。
回答by Marcio Paulo
I believe there is a simpler solution.
You must use Html.Hiddeninstead of Html.HiddenFor. Look:
我相信有一个更简单的解决方案。您必须使用Html.Hidden代替Html.HiddenFor。看:
@Html.Hidden("CRN", ViewData["crn"]);
This will create an INPUTtag of type="hidden", with id="CRN"and name="CRN", and the correct value inside the valueattribute.
这将创建一个INPUT的标签type="hidden",有id="CRN"和name="CRN",和里面的正确的价值value属性。
Hope it helps!
希望能帮助到你!
回答by RickAndMSFT
Keep in mind the second parameter to @Html.HiddenFor will only be used to set the value when it can't find route or model data matching the field. Darin is correct, use view model.
请记住,@Html.HiddenFor 的第二个参数仅用于在找不到与该字段匹配的路由或模型数据时设置该值。Darin 是正确的,使用视图模型。
回答by Jane Cohen
A simple answer is to use @Html.TextboxFor but place it in a div that is hidden with style. Example: In View:
一个简单的答案是使用 @Html.TextboxFor 但将它放在一个隐藏的 div 中。示例:在视图中:
<div style="display:none">
@Html.TextboxFor(x=>x.CRN)
</div>
<div style="display:none">
@Html.TextboxFor(x=>x.CRN)
</div>

