asp.net-mvc 隐藏因为没有从视图模型中获得正确的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4837744/
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
Hiddenfor not getting correct value from view model
提问by user427875
I have a multi-step file import process. I have a hidden form input in my view that I am trying to populate with the "CurrentStep" from the view model.
我有一个多步骤的文件导入过程。我的视图中有一个隐藏的表单输入,我试图用视图模型中的“CurrentStep”进行填充。
<% = Html.HiddenFor(model => model.CurrentStep) %>
CurrentStep is an Enum and I always get the default value rather than the one I provided to the view model. on the other hand this gets me the correct value:
CurrentStep 是一个枚举,我总是得到默认值,而不是我提供给视图模型的值。另一方面,这让我得到了正确的价值:
<p><% = Model.CurrentStep %></p>
I realise I could just hand code the hidden input but I want to know: what am I doing wrong? Is there a better way to keep track of the current step between POSTs?
我意识到我可以手动编码隐藏的输入,但我想知道:我做错了什么?有没有更好的方法来跟踪 POST 之间的当前步骤?
回答by Darin Dimitrov
What you are doing wrong is that you are trying to modify the value of a POSTed variable in your controller action. So I suppose you are trying to do this:
您做错了什么是您试图修改控制器操作中 POSTed 变量的值。所以我想你正在尝试这样做:
[HttpPost]
public ActionResult Foo(SomeModel model)
{
model.CurrentStep = Steps.SomeNewValue;
return View(model);
}
and html helpers such as HiddenFor will always first use the POSTed value and after that the value in the model.
和诸如 HiddenFor 之类的 html 助手将始终首先使用 POSTed 值,然后是模型中的值。
So you have a couple of possibilities:
所以你有几种可能性:
Remove the value from the modelstate:
[HttpPost] public ActionResult Foo(SomeModel model) { ModelState.Remove("CurrentStep"); model.CurrentStep = Steps.SomeNewValue; return View(model); }
Manually generate the hidden field
<input type="hidden" name="NextStep" value="<%= Model.CurrentStep %>" />
Write a custom helper which will use the value of your model and not the one that's being POSTed
从模型状态中删除值:
[HttpPost] public ActionResult Foo(SomeModel model) { ModelState.Remove("CurrentStep"); model.CurrentStep = Steps.SomeNewValue; return View(model); }
手动生成隐藏字段
<input type="hidden" name="NextStep" value="<%= Model.CurrentStep %>" />
编写一个自定义助手,它将使用您的模型的值而不是正在发布的值
回答by Zac
My solution was to use Darin's second option, because option 1 (clearing from the model state) means hard coding a string (and the naming convention can be tricky with complex models), and wanted to avoid option 3 because I already have so many custom helpers.
我的解决方案是使用 Darin 的第二个选项,因为选项 1(从模型状态中清除)意味着对字符串进行硬编码(并且命名约定对于复杂模型可能很棘手),并且想要避免选项 3,因为我已经有很多自定义帮手。
<input type="hidden" name="@Html.NameFor(x => Model.SomeId)" value="@Model.SomeId" />
Just a reminder that you can use Html.NameFor
to keep things clean.
只是一个提醒,您可以使用它Html.NameFor
来保持清洁。
回答by ReBoot The Universe
Make sure you model property has a "set" operator.
确保您的模型属性具有“设置”运算符。
This won't get updated on post-back:
这不会在回传时更新:
@Html.HiddenFor( m => m.NoSeq)
@Html.HiddenFor( m => m.NoSeq)
public Class MyModel
{
int _NoSeq;
public NoSeq
{
get { return _NoSeq };
}
}