- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们在 MVC3 中遇到 Html.HiddenFor 的问题偶尔无法正确绑定(bind)。我们根本无法重现它,但我们在日志记录中看到了空引用,这让我们彻底发疯。
我们有以下模型和 Controller 结构:
public class DummyController
{
[HttpGet]
public ActionResult ReturnAPage(int NumericID)
{
//NumericID should never be 0 or negative, but let's check to make sure
if (NumericID < 1)
{
return RedirectToAction("TracyJordanStabbingRobot");
}
return View("DummyView", new DummyViewModel(NumericID));
}
[HttpPost]
public ActionResult TakePageSubmission(DummyViewModel model)
{
//AnObject relies on having a non-zero ID
ComplexObject AnObject = new ComplexObject(model.NumericID);
AnObject.UseMe();
}
}
public class DummyViewModel
{
public DummyViewModel() {}
public DummyViewModel(int ID)
{
NumericID = ID;
}
public int NumericID { get; set; }
}
...以及以下 View 结构:
DummyView.cshtml
@model DummyViewModel
<html>
<head></head>
<body>
<p>THIS IS A VIEW!</p>
<form id="DummyViewForm" action="/RouteTo/TakePageSubmission" method="post">
@Html.Partial("_PartialDummyView", Model)
<input type="submit" value="Submit This!" />
</form>
</body>
</html>
_PartialDummyView.cshtml
@model DummyViewModel
<p>Heard you like views...</p>
@Html.HiddenFor(model => model.NumericID)
考虑到我们在初始 Controller 操作中检查小于零的值,因此 @Html.HiddenFor(model => model.NumericID)
永远不应该有小于零的值- 大于零的值。
话虽这么说,当我们在 TakePageSubmission
操作中使用 AnObject
时,我们会收到空引用错误。
当我们深入记录 model.NumericID
值时,我们发现它为零,考虑到 DummyView 只能是,这应该不可能使用非零值访问。
我们有点困惑,因为我们无法可靠地重现该问题,所以我们不知道可能是什么原因造成的。有人遇到过类似的事情吗?
编辑:我们正在对表单帖子进行 ModelState 验证,但我们不会检查 NumericID 是否为 0。当我们检查时,模型是无效的,这只是证明HiddenFor 设置不正确。此外,页面的路由实际上包括 NumericID,因此,例如,我们已经看到这种情况发生在:
http://our.site.com/RouteToReturnAPage/1736/
...如果明确设置了操作参数,则模型构建正确,但由于某种未知原因,HiddenFor NumericID 值为 0。这确实令人困惑。
最佳答案
您的默认 0 值绑定(bind)是从 MVC View'ing 到发布后的同一页面,认为由于发布期间的错误而重新加载相同的 View 。正确的绑定(bind)将发生在对不同操作调用的加载/操作调用上。
有一种破解方法,可以在重新加载 View 之前使用 ModelState.Clear();
。
此外,根本不使用帮助程序来创建隐藏字段,例如:
<input type="hidden" value="@Model.NumericID" id="NumericID" name="NumericID" />
关于asp.net-mvc - Html.HiddenFor 偶尔无法设置模型信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15573531/
我有 4 个 TextView。它们一开始都是不可见的,我将它们动画到屏幕上(从底部滑入)。我启动一个线程来为每个动画制作动画,每个动画开始时间间隔 200 毫秒。问题是:开始 Activity 后第
我有一个奇怪的问题,我似乎无法解决。 :(我有一个基于 Web 的应用程序,可以发送电子邮件。它通过连接在本地网络上设置的基于 Windows 的 SMTP 服务器来实现。此 SMTP 服务器不需要我
从昨天开始,我一直遇到一些设备收不到推送通知的问题。证书/设备 token 似乎是正确的,因为直到昨天,该设备还可以成功接收推送通知。 在服务器端,没有错误或连接拒绝,而且推送通知似乎每次都发送成功。
我有一个将 Angular 错误记录到数据库的应用程序,并且我偶尔会看到此错误的日志 Error: [$compile:tpload] http://errors.angularjs.org/1.5.
我收到一个 ImportError from azure.storage.blob import BlobServiceClient 但是我意识到当我安装依赖项时会发生错误 pip install a
我已经完成了使用AVAudioPlayer实现的简单音频播放GUI。 播放声音时,我使用UISlider提供播放反馈... 这是奇怪的地方。 我有一个非常偶尔发生的问题-而且大多数情况都不会发生,因此
在我的 XNA 游戏中,我的计算机(2.5 Ghz 双核 Intel Centrino 2)在 Debug模式下有一个大约需要 10 毫秒的操作。它只会每隔几分钟运行一次,它基本上只是整数数学/按位运
我相信我的应用程序编码正确(至少大部分是这样),因为它在大约 98% 的时间里都能正常工作。但是,大约 2% 的时间,我得到的似乎是有效的身份验证 token ,但是当我去发布照片时,我收到以下错误“
我在 apple store 上有一个应用程序,在 iOS6 更新后,我在 MKMapView 中收到了数百份崩溃报告。我无法在我的设备上重现崩溃。它看起来像是 EAGLContext 的问题。我们不
我最近开始使用 Foundation 5(我第一次使用 Foundation),我遇到了一个问题,似乎任何元素都响应了 .click 事件(例如顶部导航栏,单击元素时显示的数据下拉列表)将随机不起作用
花了大约一整天的时间试图解决这个问题,希望有人能帮助我!我是新来的,所以如果这是一个重复的问题,我深表歉意(我做了我的研究,但无法提前找到任何东西)。 我正在为我的咨询公司构建的网站使用浏览器缓存,但
我有一个表格,使用了以下 CSS 和 HTML: .price { position:relative; display:block; text-align:center; }
我们使用 ansible 将多个节点配置为一个集群。这些机器是在自定义 AWS 类似基础设施上创建的实例。 我们在不同的剧本上有大约一百个任务,它们在每个节点上执行。 问题是,我们收到零星的主机无法访
我们偶尔会从用户那里收到崩溃报告(我们使用崩溃报告程序包将崩溃发送到我们的服务器),并出现以下错误: “非法尝试在不同上下文中的对象之间建立关系‘...’” (不同的报表可能有不同的关系,并非所有报表
我是一名优秀的程序员,十分优秀!