c# – MVC3模型绑定 – 列表到隐藏字段
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – MVC3模型绑定 – 列表到隐藏字段,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4332字,纯文字阅读大概需要7分钟。
内容图文
![c# – MVC3模型绑定 – 列表到隐藏字段](/upload/InfoBanner/zyjiaocheng/762/7c1063a0761c4a7f90774adb7e178537.jpg)
我有一个特殊的问题 – 我有一个带有List的ViewModel用于显示图像列表:
public List<int> TrackerKeys { get; set; }
这在页面的两个位置使用:
@for (int i = 0; i < Model.TrackerKeys.Count(); i++)
{
@Html.GenerateImage(PageModes.Http, Model.TrackerKeys[i])
}
并且
@for (int i = 0; i < Model.TrackerKeys.Count(); i++)
{
@Html.HiddenFor(model => model.TrackerKeys[i])
}
这是在表单中 – 当提交表单时,如果发生验证错误,则使用新的随机数字集更新TrackerKeys属性.我正在传回上一个列表,以确保用户不会再次看到相同的图像.
跟踪器键已正确设置并传递回视图.
我的问题是:
图像根据列表中的新值正确显示新图像
然而
隐藏字段的值不会更新为新值 – 它们保留原始值.
有任何想法吗?这是MVC3的错误吗?任何投入将不胜感激.谢谢.
编辑
提交前的HTML:
<img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/26.gif" width="35" />
<img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/33.gif" width="35" />
<img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/8.gif" width="35" />
<img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/30.gif" width="35" />
<img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/6.gif" width="35" />
<img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/18.gif" width="35" />
和
<input id="TrackerKeys_0_" name="TrackerKeys[0]" type="hidden" value="26" />
<input id="TrackerKeys_1_" name="TrackerKeys[1]" type="hidden" value="33" />
<input id="TrackerKeys_2_" name="TrackerKeys[2]" type="hidden" value="8" />
<input id="TrackerKeys_3_" name="TrackerKeys[3]" type="hidden" value="30" />
<input id="TrackerKeys_4_" name="TrackerKeys[4]" type="hidden" value="6" />
<input id="TrackerKeys_5_" name="TrackerKeys[5]" type="hidden" value="18" />
之后:
在这里纠正新的价值……
<img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/16.gif" width="35" />
<img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/20.gif" width="35" />
<img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/11.gif" width="35" />
<img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/19.gif" width="35" />
<img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/26.gif" width="35" />
<img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/15.gif" width="35" />
和…
仍保留旧价值……
<input id="TrackerKeys_0_" name="TrackerKeys[0]" type="hidden" value="26" />
<input id="TrackerKeys_1_" name="TrackerKeys[1]" type="hidden" value="33" />
<input id="TrackerKeys_2_" name="TrackerKeys[2]" type="hidden" value="8" />
<input id="TrackerKeys_3_" name="TrackerKeys[3]" type="hidden" value="30" />
<input id="TrackerKeys_4_" name="TrackerKeys[4]" type="hidden" value="6" />
<input id="TrackerKeys_5_" name="TrackerKeys[5]" type="hidden" value="18" />
控制器动作
[HttpPost]
public ActionResult EmployerRegistration(EmployerViewModel Model)
{
if (ModelState.IsValid)
{
//do bits here
}
Model.TrackerKeys = Helper.GenerateNewNumbers(Model.TrackerKeys);
return View(Model);
}
解决方法:
Is this a bug with MVC3?
哦,不,这是设计的,它是所有HTML助手在ASP.NET MVC中的工作方式. HTML帮助程序(例如Html.TextBoxFor,Html.HiddenFor,…)在绑定它们的值时首先查看ModelState,然后查看Model.因此,如果您打算在POST控制器操作中修改模型的某些属性,并且此属性是POST主体的一部分,则必须首先从ModelState中删除旧值:
[HttpPost]
public ActionResult EmployerRegistration(EmployerViewModel Model)
{
if (ModelState.IsValid)
{
//do bits here
}
// we clear all values from the modelstate => this will ensure that
// the helpers will use the values from the model and not those that
// were part of the initial POST.
ModelState.Clear();
Model.TrackerKeys = Helper.GenerateNewNumbers(Model.TrackerKeys);
return View(Model);
}
或者如果您不想清除整个ModelState(因为这将删除可能与它们关联的所有值和任何相应的模型状态错误),您只能删除实际修改的那些键:
[HttpPost]
public ActionResult EmployerRegistration(EmployerViewModel Model)
{
if (ModelState.IsValid)
{
//do bits here
}
for (var i = 0; i < Model.TrackerKeys.Count; i++)
{
ModelState.Remove(string.Format("TrackerKeys[{0}]", i));
}
Model.TrackerKeys = Helper.GenerateNewNumbers(Model.TrackerKeys);
return View(Model);
}
内容总结
以上是互联网集市为您收集整理的c# – MVC3模型绑定 – 列表到隐藏字段全部内容,希望文章能够帮你解决c# – MVC3模型绑定 – 列表到隐藏字段所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。