ASP.NET MVC:隐藏字段的值没有被使用渲染HtmlHelper.Hidden [英] ASP.NET MVC: Hidden field value does not get rendered using HtmlHelper.Hidden

查看:383
本文介绍了ASP.NET MVC:隐藏字段的值没有被使用渲染HtmlHelper.Hidden的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

东西pretty的奇怪的是发生与我的应用程序:

Something pretty weird is happening with my app:

我在我的ViewModel以下属性:

I have the following property in my ViewModel:

public int? StakeholderId { get; set; }

它得到了的部分的观点呈现如下:

<%= Html.Hidden("StakeholderId", Model.StakeholderId) %>

表单提交,以及相关的控制器动作产生一个ID,并且更新了模型,返回相同的观点与更新的模型

The form is submitted, and the relevant controller action generates an id and updates the model, before returning the same view with the updated model

我遇到的问题是,隐藏字段没有在其价值什么属性呈现即使StakeholderId现在有一个值的第二次。

The problem I'm experiencing is that the hidden field does not have anything in its "value" attribute rendered the second time even though StakeholderId now has a value.

如果我只输出自己的价值,它显示在页面上,让我知道了通过这样做是为了使值:

If I just output the value on its own, it shows up on the page, so I've got it to render the value by doing this:

<input type="hidden" id="StakeholderId" name="stakeholderId" value="<%: Model.StakeholderId %>" />

但它是pretty的奇怪的是,帮助不拿起更新值?

But it's pretty strange that the helper doesn't pick up the updated value?

(我使用jQuery提交表单和渲染操作结果为申报单,但我检查,我回来了HTML已经是错了jQuery不会与任何东西之前,所以我不认为有很多做任何事情)

(I'm using jQuery to submit forms and render the action results into divs, but I've checked and the html I get back is already wrong before jQuery does anything with it, so I don't think that has much to do with anything)

更新

因为我已经发现,我还可以清除相关的ModelState键之前我的控制器动作返回的局部视图。

I've since discovered that I can also clear the relevant ModelState key before my controller action returns the partial view.

推荐答案

该助手将首先要看所张贴的价值观和使用它们。当你张贴的形式,它会拿起ID的旧值。你的解决方法是正确的。

The helper will first look for POSTed values and use them. As you are posting the form it will pick up the old value of the ID. Your workaround is correct.

这篇关于ASP.NET MVC:隐藏字段的值没有被使用渲染HtmlHelper.Hidden的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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