C# 回发后隐藏的表单字段未出现在MVC模型中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9776312/
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
Hidden form fields not appearing in MVC Model after post-back
提问by JoeGeeky
I have a new MVC 4 Application with a fairly basic View/Controller. The associated Model contains a couple properties that I've mapped to Hiddenform fields. When the Page renders the first time (e.g. via the HttpGet Action) it all looks fine. But when the form is Post'ed by selecting the Submit button the resulting Model presented to the Action no longer has the Hidden field values set. Here is a walkthrough of the particulars.
我有一个带有相当基本的视图/控制器的新 MVC 4 应用程序。关联的模型包含我已映射到隐藏表单字段的几个属性。当页面第一次呈现时(例如通过 HttpGet 操作),一切看起来都很好。但是当通过选择提交按钮发布表单时,呈现给操作的结果模型不再设置隐藏字段值。这是详细信息的演练。
Here is a sample of the Model:
这是模型的示例:
public class Application
{
public bool ShowSideBars { get; set; }
}
Here is the initial Controller*Action* (which seems to work fine):
这是初始的Controller* Action* (似乎工作正常):
[HttpGet]
public ActionResult Application()
{
var model = Request.ParseFromQueryString<Application>();
model.ShowSideBars = true;
return View(model);
}
This maps to the Viewas follows:
这映射到视图如下:
<fieldset>
@Html.HiddenFor(m => m.ShowSideBars)
...
</fieldset>
This results in the following mark-upto be rendered inside the fieldset:
这将导致在字段集中呈现以下标记:
<input data-val="true" data-val-required="The ShowSideBars field is required." id="ShowSideBars" name="ShowSideBars" type="hidden" value="True" />
Note: I sure wish I knew why MVC has decided to add the '... field is required' content when I didn't flag it as required, but that's for another question
注意:当我没有将其标记为必需时,我当然希望我知道为什么 MVC 决定添加“...字段是必需的”内容,但这是另一个问题
Here is the Actionthat is called when the form is submitted. At this point the aforementioned property will no longer be set to 'true'.
这是提交表单时调用的Action。此时,上述属性将不再设置为“ true”。
[HttpPost]
public ActionResult Application(Application application)
{
// Other work done here
return View(application);
}
At present, there are no custom Model Binders. Also, I've tested some other data types and I'm seeing the same thing.
目前,没有自定义模型绑定器。此外,我测试了一些其他数据类型,我看到了同样的事情。
Can someone explain why hidden form values are not being returned? Am I just doing this all wrong?
有人可以解释为什么没有返回隐藏的表单值吗?我只是做错了吗?
采纳答案by Darin Dimitrov
I cannot reproduce the issue (ASP.NET MVC 4 Beta running on VS 2010 .NET 4.0).
我无法重现该问题(在 VS 2010 .NET 4.0 上运行的 ASP.NET MVC 4 Beta)。
Model:
模型:
public class Application
{
public bool ShowSideBars { get; set; }
}
Controller:
控制器:
public class HomeController : Controller
{
public ActionResult Application()
{
var model = new Application();
model.ShowSideBars = true;
return View(model);
}
[HttpPost]
public ActionResult Application(Application application)
{
return Content(application.ShowSideBars.ToString());
}
}
View:
看法:
@model Application
@using (Html.BeginForm())
{
@Html.HiddenFor(m => m.ShowSideBars)
<button type="submit">OK</button>
}
When I submit the form, the model binder correctly assigns the ShowSideBarsproperty in the POST action to true.
当我提交表单时,模型绑定器正确地将ShowSideBarsPOST 操作中的属性分配为 true。
Note: I sure wish I knew why MVC has decided to add the '... field is required' content when I didn't flag it as required, but that's for another question
注意:当我没有将其标记为必需时,我当然希望我知道为什么 MVC 决定添加“...字段是必需的”内容,但这是另一个问题
That's because non-nullable types such as booleans are always required. You could stop ASP.NET MVC helpers from emitting HTML5 data-* client side validation attributes for them by putting the following line in Application_Start:
那是因为总是需要不可为空的类型,例如布尔值。您可以通过将以下行放入来阻止 ASP.NET MVC 助手为它们发出 HTML5 data-* 客户端验证属性Application_Start:
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
回答by Richard Beverly
If you have the property in your model decorated with a ReadOnlyAttribute the value will not be populated back into the model for you. After all, it is read only.
如果您的模型中的属性使用 ReadOnlyAttribute 修饰,则该值将不会为您重新填充到模型中。毕竟,它是只读的。
回答by Ludwo
try this:
尝试这个:
public class Model
{
[ScaffoldColumn(false)]
public bool InvisibleProperty { get; set; }
}
more info here (ScaffoldColumn(bool value) vs HiddenInput(DisplayValue = bool value) in MVC)
此处有更多信息(MVC 中的 ScaffoldColumn(bool value) 与 HiddenInput(DisplayValue = bool value))
回答by Ravid Goldenberg
I just had the same problem. The form didn't submitted the hidden property because the model class didn't had a proper getter and setterfor that property. I know that is not the issue you had, just figured it might help other people that will lend in this page.
我只是遇到了同样的问题。表单没有提交隐藏属性,因为模型类没有该属性的适当getter 和 setter。我知道这不是您遇到的问题,只是想它可能会帮助将在此页面上借出的其他人。
回答by diegosasw
I think the fields MUST be within the form html tags for the hidden ones to be posted back and not ignored
我认为这些字段必须在表单 html 标签内,以便隐藏的被回发而不是被忽略
回答by toddmo
In my case it was because I had declared a field instead of a property:
就我而言,这是因为我声明了一个字段而不是一个属性:
public BaseController.Modes Mode;
doesn't work. But:
不起作用。但:
public BaseController.Modes Mode { get; set; }
works. The default model binder only works with properties.
作品。默认模型绑定器仅适用于属性。
回答by Jess
I kid you not, this is another reason it could happen.
我没有骗你,这是它可能发生的另一个原因。
My form had the same field in it twice.The other field was actually not in the form, but that doesn't matter.
我的表单中有两次相同的字段。另一个字段实际上不在表单中,但这并不重要。
Run this jQuery in the developer console to see how many elements come back:
在开发者控制台中运行此 jQuery 以查看返回的元素数量:
$("[id$=PropertyName]"); // Search for ids ending with property name.
Example:
例子:


