简体   繁体   English

回发后未出现在 MVC Model 中的隐藏表单字段

[英]Hidden form fields not appearing in MVC Model after post-back

I have a new MVC 4 Application with a fairly basic View/Controller.我有一个带有相当基本视图/控制器的新 MVC 4 应用程序。 The associated Model contains a couple properties that I've mapped to Hidden form fields.关联的 Model 包含我已映射到隐藏表单字段的几个属性。 When the Page renders the first time ( eg via the HttpGet Action ) it all looks fine.当页面第一次呈现时(例如通过 HttpGet Action ),一切看起来都很好。 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.但是,当通过选择提交按钮发布表单时,呈现给操作的结果 Model 不再设置隐藏字段值。 Here is a walkthrough of the particulars.这是详细信息的演练。

Here is a sample of the Model :这是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 View as follows:这映射到视图如下:

<fieldset>
    @Html.HiddenFor(m => m.ShowSideBars)
...
</fieldset>

This results in the following mark-up to 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 Action that is called when the form is submitted.这是提交表单时调用的操作 At this point the aforementioned property will no longer be set to ' true '.此时,上述属性将不再设置为“ true ”。

[HttpPost]
public ActionResult Application(Application application)
{
    // Other work done here

    return View(application);
}

At present, there are no custom Model Binders.目前,没有定制的 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?我只是做错了吗?

If you have the property in your model decorated with a ReadOnlyAttribute the value will not be populated back into the model for you. 如果模型中的属性使用ReadOnlyAttribute修饰,则不会将值填充回模型中。 After all, it is read only. 毕竟,它是只读的。

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 ShowSideBars property in the POST action to true. 当我提交表单时,模型绑定器正确地将POST操作中的ShowSideBars属性指定为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决定在我没有按要求标记时添加'... field is required'内容,但那是另一个问题

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 : 您可以通过在Application_Start以下行来阻止ASP.NET MVC帮助程序为它们发出HTML5 data- *客户端验证属性:

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

我认为字段必须在html标签的形式内,以便隐藏的字段被回发而不被忽略

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 setter for that property. 表单没有提交隐藏属性,因为模型类没有为该属性提供适当的getter和setter I know that is not the issue you had, just figured it might help other people that will lend in this page. 我知道这不是你的问题,只是认为它可能会帮助其他人在这个页面中借出。

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) 更多信息(ScaffoldColumn(bool值)vs MVC中的HiddenInput(DisplayValue = bool值))

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. 默认模型绑定器仅适用于属性。

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: 例:

在此输入图像描述

For me, in Core 6 the solution was removing [Editable(false)] attribute from the model class Id property which I wanted to tunnel through get/post as a hidden form field.对我来说,在 Core 6 中,解决方案是从 model class Id 属性中删除[Editable(false)]属性,我想通过 get/post 作为隐藏表单字段进行隧道传输。 In.Net 4.8 it was not a problem.在.Net 4.8 中这不是问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM