MVC 4 @ HTML.HiddenFor没有更新上回发 [英] MVC 4 @HTML.HiddenFor are not updating on a postback

查看:137
本文介绍了MVC 4 @ HTML.HiddenFor没有更新上回发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经与视图状态问题上的一系列的页面访问量 - 在一个页面的剃刀初始视图我使用 Html.HiddenFor 如下:

  @ Html.HiddenFor(X => Model.err)
    @ Html.HiddenFor(X => Model.errField)
    @ Html.HiddenFor(X => Model.errMessage)
    @ Html.HiddenFor(X => Model.IsMove)

这似乎很好地工作。我隐藏输入标签包含正确的值。然而,当我提交表单 [HTTPPost] ,并在我的控制器动作更新模型。

  model.err = transHelper.err;
       model.errField = transHelper.errField;
       model.errMessage = transHelper.errMessage;
       返回查看(模型);

隐藏字段似乎并不更新,它们包含来自初始视图原始值。然而,当我使用这些字段在另一个方面相同的Razor视图中像这样...

  @ *
        这似乎不能正确更新...    @ Html.HiddenFor(X => Model.err)
    @ Html.HiddenFor(X => Model.errField)
    @ Html.HiddenFor(X => Model.errMessage)
    @ Html.HiddenFor(X => Model.IsMove)        * @
        <输入类型=隐藏ID =ERRVALUE =@ Model.err/>
        <输入类型=隐藏ID =errFieldVALUE =@ Model.errField/>
        <输入类型=隐藏ID =errMessageVALUE =@ Model.errMessage/>
        <输入类型=隐藏ID =IsMoveVALUE =@ Model.IsMove/>    < / DIV>

然后输入字段正确更新。我甚至创造了一个视图助手来帮助调试,并在所有情况下,模型似乎已经在的HtmlHelper&LT正确性;的TModel> - 我甚至返回的型号为返回JSON(模型); 和数据的罚款。

在这一点上,我对工作四处奔跑,但没有任何人知道为什么 @ Html.HiddenFor 是脏的。

更新:这里是我的控制器动作

  [HttpPost]
   公众的ActionResult指数(HomePageModel模型)
   {
       //交易流程
       Transactionr transr =新Transactionr();
       transr.Process(模型);       model.err = transr.err;
       model.errField = transr.errField;
       model.errMessage = transr.errMessage;       返回查看(模型);
   }

下面是我的看法:

  @model App.Models.HomePageModel
    @ {
        ViewBag.Title =产品分类程序;
    }
    <表ID =FORMDATA方法=邮报行动=/首页/索引>
        @ Html.AntiForgeryToken()
        <&字段集GT;
            < D​​IV>            @ Html.HiddenFor(型号=> model.err)
            @ Html.HiddenFor(型号=> model.errField)
            @ Html.HiddenFor(型号=> model.errMessage)
            @ Html.HiddenFor(型号=> model.IsMove)            <输入类型=隐藏ID =myerrVALUE =@ Model.err/>
            <输入类型=隐藏ID =myerrFieldVALUE =@ Model.errField/>            < / DIV>           < D​​IV CLASS =栏目组>
                < D​​IV CLASS =山坳span_2_of_2>
                     < D​​IV CLASS =消息ID =消息>
                         @if(Model.err℃,)
                         {
                             <跨度风格=颜色:紫色;> @ Model.errMessage(@ Model.err) - (@ Model.errField)LT; / SPAN>
                         }
                         否则如果(Model.err大于0)
                         {
                             <跨度风格=颜色:红色;> @ Model.errMessage(@ Model.err)(@ Model.errField)LT; / SPAN>
                         }其他{
                            <跨度> @ Model.errMessage(@ Model.err)(@ Model.errField)LT; / SPAN>
                         }
                         < / DIV>
                     < / DIV>
            < / DIV>            < D​​IV CLASS =栏目组ID =工作区>
                  @ Html.Partial(_ WorkspacePartial模型)
            < / DIV>
              < D​​IV CLASS =栏目组ID =细节>
                  @ Html.Partial(_ DetailPartial模型)
              < / DIV>
        < /字段集>
        < /表及GT;

下面是我的模型:

 公共类HomePageModel
 {
    公众诠释FromStore {搞定;组; }    //复制/移动交易的to部分
    公众诠释ToStore {搞定;组; }    //复制/移动的交易清单
    公开名单< INT>详细信息{搞定;组; }
    //真的是假的移动副本是
    公共BOOL IsMove {搞定;组; }    //当前消息
    公众诠释ERR {搞定;组; }
    公众诠释errField {搞定;组; }
    公共字符串errMessage {搞定;组; }


解决方案

默认HtmlHelpers行为(@ Html.HiddenFor等)是做出你所描述的。

即。在后您的视图模型的任何更改都付诸行动,你从后返回的视图中接收到的任何变化,但HTMLHELPERS上重新呈现时,previous后的值在precedence在改变视图模型值。

想修理在一个快速+肮脏的方式这个问题,清除ModelState.Clear()返回从之前的HttpPost ActionMethod!

Having issues with the view state on a series of page views -- On the initial view of a page in Razor I am using Html.HiddenFor as follows:

    @Html.HiddenFor(x => Model.err)
    @Html.HiddenFor(x => Model.errField)
    @Html.HiddenFor(x => Model.errMessage)
    @Html.HiddenFor(x => Model.IsMove)

which seems to work fine. My hidden input tags contain the correct values. However when I submit the form [HTTPPost] and update the model in my controller action with..

       model.err = transHelper.err;
       model.errField = transHelper.errField;
       model.errMessage = transHelper.errMessage;
       return View(model);

The hidden fields do not seem to update, they contain the original values from the initial view. However When I use these fields in another context within the same razor view like this...

     @*      
        this seems to not update correctly...

    @Html.HiddenFor(x => Model.err)
    @Html.HiddenFor(x => Model.errField)
    @Html.HiddenFor(x => Model.errMessage)
    @Html.HiddenFor(x => Model.IsMove)

        *@
        <input type="hidden" id="err" value="@Model.err" />
        <input type="hidden" id="errField" value="@Model.errField" />
        <input type="hidden" id="errMessage" value="@Model.errMessage" />
        <input type="hidden" id="IsMove" value="@Model.IsMove" />

    </div>

Then the input fields update correctly. I even created a View Helper to help debug, and in all cases, the Model seems to have correct data in HtmlHelper<TModel> -- I even returned the Model as return Json(model); and the data was fine.

At this point I am running with the work around, but does anybody know why @Html.HiddenFor is dirty.

Update: here is my controller actions

  [HttpPost]
   public ActionResult Index(HomePageModel model)
   {


       // process transaction
       Transactionr transr = new Transactionr();
       transr.Process(model);

       model.err = transr.err;
       model.errField = transr.errField;
       model.errMessage = transr.errMessage;

       return View(model);
   }

Here is my view:

        @model App.Models.HomePageModel
    @{
        ViewBag.Title = "Product Categorizer";
    }
    <form id="formData" method="post" action="/Home/Index">
        @Html.AntiForgeryToken()
        <fieldset>
            <div>

            @Html.HiddenFor(model => model.err)
            @Html.HiddenFor(model => model.errField)
            @Html.HiddenFor(model => model.errMessage)
            @Html.HiddenFor(model => model.IsMove)

            <input type="hidden" id="myerr" value="@Model.err" />
            <input type="hidden" id="myerrField" value="@Model.errField" />

            </div>

           <div class="section group">
                <div class="col span_2_of_2">
                     <div class="message" id ="message">
                         @if (Model.err < 0)
                         {
                             <span style="color: purple;">@Model.errMessage (@Model.err) - (@Model.errField)</span>
                         }
                         else if (Model.err > 0)
                         {
                             <span style="color:red;">@Model.errMessage (@Model.err) (@Model.errField)</span>
                         } else {
                            <span>@Model.errMessage (@Model.err) (@Model.errField)</span>
                         }
                         </div>
                     </div>
            </div>

            <div class="section group" id="workspace">
                  @Html.Partial("_WorkspacePartial", Model)
            </div>
              <div class="section group" id="details">
                  @Html.Partial("_DetailPartial", Model)
              </div>


        </fieldset>
        </form>

Here is my model:

 public class HomePageModel
 {
    public int FromStore { get; set; }

    //  the "To" part of the copy/move transaction
    public int ToStore { get; set; }

    // a list of the copy/move transaction
    public List<int> Details { get; set; }


    // true is move false is copy
    public bool IsMove { get; set; }

    // current message
    public int err { get; set; }
    public int errField { get; set; }
    public string errMessage { get; set; }

解决方案

The default HtmlHelpers behavior (@Html.HiddenFor, etc) is to behave exactly as you have described.

i.e. any changes you make to the ViewModel on a post are actioned, any changes you return from the Post are received by the view, but on re-rendering WITH HTMLHELPERS, the previous Post-values take precedence over the changed ViewModel values.

Want to "fix" this behavior in a quick + dirty way, clear the ModelState.Clear() prior to returning from the HttpPost ActionMethod !

这篇关于MVC 4 @ HTML.HiddenFor没有更新上回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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