C# ASP.NET MVC:隐藏字段值不会使用 HtmlHelper.Hidden 呈现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2019131/
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
ASP.NET MVC: Hidden field value does not get rendered using HtmlHelper.Hidden
提问by Veli Gebrev
Something pretty weird is happening with my app:
我的应用程序发生了一些非常奇怪的事情:
I have the following property in my ViewModel:
我的 ViewModel 中有以下属性:
public int? StakeholderId { get; set; }
It gets rendered in a partialview as follows:
它在局部视图中呈现如下:
<%= Html.Hidden("StakeholderId", Model.StakeholderId) %>
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
提交表单,相关的控制器动作生成一个 id 并更新模型,然后返回与更新模型相同的视图
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.
我遇到的问题是,即使 StakeholderId 现在有值,隐藏字段的“值”属性中没有任何内容第二次呈现。
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 %>" />
But it's pretty strange that the helper doesn't pick up the updated value?
但是很奇怪,助手没有获取更新的值?
(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)
(我正在使用 jQuery 提交表单并将操作结果呈现到 div 中,但是我已经检查过并且在 jQuery 对其进行任何处理之前返回的 html 已经是错误的,所以我认为这与任何事物)
UPDATE
更新
I've since discovered that I can also clear the relevant ModelState key before my controller action returns the partial view.
从那以后,我发现我还可以在控制器操作返回局部视图之前清除相关的 ModelState 键。
采纳答案by Darin Dimitrov
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.
助手将首先查找 POSTed 值并使用它们。当您发布表单时,它将获取 ID 的旧值。您的解决方法是正确的。
回答by awrigley
ADDENDUM: Multiple HTML Forms, eg, in a Grid
附录:多个 HTML 表单,例如,在一个网格中
As an addendeum to this issue, one thing to be VERY careful of is with multiple forms on the same page, eg, in a grid, say one generated using Ajax.BeginForm.
作为这个问题的补充,需要非常小心的一件事是在同一页面上有多个表单,例如,在一个网格中,比如一个使用 Ajax.BeginForm 生成的表单。
You might be tempted to write something along the lines of:
你可能会想写一些类似的东西:
@foreach (var username in Model.TutorUserNames)
{
<tr>
<td>
@Html.ActionLink(username, MVC.Admin.TutorEditor.Details(username))
</td>
<td>
@using (Ajax.BeginForm("DeleteTutor", "Members",
new AjaxOptions
{
UpdateTargetId = "AdminBlock",
OnBegin = "isValidPleaseWait",
LoadingElementId = "PleaseWait"
},
new { name = "DeleteTutorForm", id = "DeleteTutorForm" }))
{
<input type="submit" value="Delete" />
@Html.Hidden("TutorName", username)
}
</td>
</tr>
}
The lethal line in here is:
这里的致命线是:
@Html.Hidden("TutorName", username)
... and intend to use TutorName as your action's parameter. EG:
...并打算使用 TutorName 作为您的操作参数。例如:
public virtual ActionResult DeleteTutor(string TutorName){...}
If you do this, the nasty surprise you are in for is that Html.Hidden("TutorName", username) will, as Darin Dimitrov explains, render the last POSTed value. Ie, regardless of your loop, ALL the items will be rendered with the TutorName of the last deleted Tutor!
如果你这样做,你会感到令人讨厌的惊喜是 Html.Hidden("TutorName", username) 会,正如 Darin Dimitrov 解释的那样,呈现最后一个 POSTed 值。即,无论您的循环如何,所有项目都将使用最后删除的 Tutor 的 TutorName 呈现!
The word around, in Razor syntax is to replace the @Html.Hidden call with an explicit input tag:
在 Razor 语法中,这个词是用显式输入标签替换 @Html.Hidden 调用:
<input type="hidden" id="TutorName" name="TutorName" value='@username' />
This works as expected.
这按预期工作。
Ie:
IE:
NEVER, EVER USE Html.Hidden TO PASS A PARAMETER BACK TO YOUR ACTIONS WHEN YOU ARE USING MULTIPLE FORMS IN A GRID!!!
当您在网格中使用多个表单时,永远不要使用 Html.Hidden 将参数传递回您的操作!!!
Final Caveat:
最后警告:
When constructing your hidden input tag, you need to include both name and id, set to the same value, otherwise, at the time of writing (Feb 2011) it won't work properly. Certainly not in Google Chrome. All you get is a null parameter returned if you only have an id and no name attribute.
在构建隐藏的输入标签时,您需要包含 name 和 id,并将其设置为相同的值,否则,在撰写本文时(2011 年 2 月),它将无法正常工作。当然不是在谷歌浏览器中。如果您只有一个 id 而没有 name 属性,那么您得到的只是一个空参数。