通过HTML Helper在“表单发布"中的"MVC隐藏"字段 [英] MVC Hidden field via HTML Helper in Form Post issue

查看:105
本文介绍了通过HTML Helper在“表单发布"中的"MVC隐藏"字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC表单帖子中使用隐藏字段时,我遇到了问题.当通过HTML Helper生成隐藏字段时,它在回发期间将不会保留其值.但是,当使用HTML标记时,它可以工作.

I have an issue when using a hidden field in an MVC Form Post. When the hidden field is generated via a HTML Helper it won't preserve it's value during the postback. But when using a HTML tag, it works.

不幸的是,这个人花了我整整一天的时间来解决这个问题.

Unfortunately this one has taken me a whole day to work out this work around.

这就是我正在做的...(对不起,任何拼写,重新输入的代码都如此):

Here is what I'm doing... (excuse any spelling, re-typed code for SO):

查看模型

public class SomeViewModel
{
    public int MyProperty1 { get; set; }
    public int MyProperty2 { get; set; }
    public int MyProperty3 { get; set; }
}

发布方法

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult MyActionMethod(SomeViewModel someViewModel, string command)
{
  ...
  ...
  // someViewModel.MyProperty1
  ...
  ...
}

查看

@using (Html.BeginForm("MyActionMethod", "SomeController", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()

    @Html.HiddenFor(m => m.MyProperty1)

    <div class="col-md-2">
        <input type="hidden" value=@Model.MyProperty1 name="MyProperty1" />
        <input type="submit" name="command" value="Button1" class="btn btn-primary" />
        <input type="submit" name="command" value="Button2" class="btn btn-success" />
    </div>
    <div class="col-md-1"></div>
    <div class="col-md-1">
        <input type="submit" name="command" value="Button3" class="btn btn-danger" />
    </div>
    <div class="col-md-8"></div>
}

在上面的查看代码中,HTML帮助器隐藏字段(@ Html.HiddenFor(m => m.MyProperty1))起作用.但是HTML标签隐藏字段(输入类型="hidden" value=@Model.MyProperty1 name ="MyProperty1")确实有效.我只启用了一个.此处显示的两个都是出于显示目的.

In the above View code, the HTML helper hidden field (@Html.HiddenFor(m => m.MyProperty1)) does NOT work. But the HTML tag hidden field (input type="hidden" value=@Model.MyProperty1 name="MyProperty1") DOES work. I only have one or the other enabled. Both shown here are for display purposes.

我更喜欢使用HTML Helper语法,但可以与HTML标记一起使用.

I'd prefer to use the HTML Helper syntax, but can live with the HTML tag.

注意事项:

  1. 视图正在使用多个提交按钮.

  1. The View is using multiple submit buttons.

该视图使用局部视图.当前,部分视图中没有内容,对此也无能为力.

The View is using a partial view. Currently no content in the partial view and nothing is being done with it.

我看不到这些将如何影响问题.以为我会提到它,以防万一.

I can't see how these would affect the issue. Thought I'd mention it, in case.

问题:有人可以解释为什么HTML Helper无法正常工作吗?

Question: Can anyone explain why the HTML Helper isn't working?

***** 更新 *****

***** UPDATE *****

感谢 Stephen Muecke 指出我的问题中需要包含的内容.此外,还要感谢您猜测我实际上在做什么,但我无法说清楚.

Thanks to Stephen Muecke for pointing out what needed to be included in my question. Moreover an extra thank you for guessing what I was actually doing but I couldn't articulate it.

我将在ActionMethod()中更新View Model属性,并且当重新渲染相同的View时,View Model属性不会反映新值. 而是保留其初始值,而不保留新值.

I'm updating the View Model property in the ActionMethod(), and when the same View is re-rendered, the View Model property doesn't reflect the new value. Rather it is keeping it's initial value, and not preserving the new value.

推荐答案

虽然不是很明显,但我发现很难找到许多与此主题相关的文章,以便在过去为我进行澄清,ASP.NET MVC中的默认行为是以下:

Although not obvious and I found it difficult to find many articles on this subject to clarify it for me in the past the default behaviour in ASP.NET MVC is the following:

如果使用的是HTML Helper,并且正在响应POST呈现同一视图,则假定您正在响应失败的表单验证. 因此,在POST中设置之前的值将始终从ModelState呈现回视图中.

If you are using HTML Helpers and you are rendering the same View in response to a POST it is assumed that you are responding to failed form validation. Therefore the values before being set in the POST will always be rendered back in the view from ModelState.

您有几种选择:

    您帖子中的
  1. ModelState.Clear();.不建议使用此框架,因为该框架不是按照这种方式设计的.
  2. 使用Post-Redirect-Get模式并仅显示验证失败,因为该框架是经过设计的(如@StephenMuecke所述).
  3. 如果您不担心验证,请不要使用HtmlHelpers
  4. 改为使用Request.Form值,并删除SomeViewModel someViewModel参数.不建议您这样做,因为您会失去模型绑定的所有好处.
  5. ModelState.Remove用于特定字段,同样不建议.
  1. ModelState.Clear(); in your post. Not recommended as the framework has not been designed this way.
  2. Use the Post-Redirect-Get pattern and only display validation failure, as the framework was designed (as @StephenMuecke mentions).
  3. If you are not bothered about validation do not use HtmlHelpers
  4. Use Request.Form values instead and remove the SomeViewModel someViewModel parameter. Wouldn't recommend this as you lose all benefits of model binding.
  5. Use ModelState.Remove for the specific field, again not recommended.

我在这方面找到的最好的文章是Simon Ince在2010年发表的文章:

The best article I found on this was article from Simon Ince in 2010:

里克·斯特拉尔(Rick Strahl)的另一个人:

Another one by Rick Strahl:

忽略模型更改的ASP.NET MVC回发和HtmlHelper控件

这篇关于通过HTML Helper在“表单发布"中的"MVC隐藏"字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆