[英]Value from hidden field is not being passed to the EF context model
I have a few values that I've put in some hidden fields of a form using Razor. 我有一些值已使用Razor放入表单的某些隐藏字段中。 When I submit the form, it does not pass the validation that Entity Framework created in the
context.tt
model of a certain field. 当我提交表单时,它没有通过实体框架在某个字段的
context.tt
模型中创建的验证。 When I submit, there is an error thrown saying that the field cannot be null. 当我提交时,有一个错误提示该字段不能为空。 This led me to believe that the value was null in the HTML.
这使我相信HTML中的值是空的。 When I checked the source this is what i found:
当我检查源时,这就是我发现的内容:
<input Value="1" data-val="true" data-val-number="The field PoolType must be a number." id="PoolType" name="PoolType" type="hidden" value="" />
<input Value="Not Complete" id="LoanStatus" name="LoanStatus" type="hidden" value="" />
<input Value="2012-12-12 09:26.39" data-val="true" data-val-required="The DateStamp field is required." id="DateStamp" name="DateStamp" type="hidden" value="" />
I'm wondering why the input
with id LoanStatus
has a value set but it is throwing an error saying that it cannot be null. 我想知道为什么ID为
LoanStatus
的input
设置了一个值,但是抛出错误,说它不能为null。 The exception thrown is: 抛出的异常是:
System.Data.ConstraintException This property cannot be set to a null value.
System.Data.ConstraintException此属性不能设置为null值。
To set the value in the HTML, I set it in the controller using the ViewBag, ViewBag.LoanStatus = "Not Complete";
要在HTML中设置值,我在控制器中使用ViewBag,
ViewBag.LoanStatus = "Not Complete";
来设置它ViewBag.LoanStatus = "Not Complete";
. 。 Then in the cshtml file
@Html.HiddenFor(model => model.LoanStatus, new { @Value = ViewBag.LoanStatus })
然后在cshtml文件
@Html.HiddenFor(model => model.LoanStatus, new { @Value = ViewBag.LoanStatus })
Is there something I am missing, I am new to MVC and Entity Framework. 有什么我缺少的东西吗,我是MVC和Entity Framework的新手。
EDIT - Additional Code More hidden elements 编辑-附加代码更多隐藏元素
@Html.HiddenFor(model => model.ID, new{ @Value = ViewBag.ID })
@Html.HiddenFor(model => model.Reason, new{ @Value = ViewBag.Reason })
@Html.HiddenFor(model => model.PoolType, new{ @Value = ViewBag.PoolType })
@Html.HiddenFor(model => model.LoanStatus, new{ @Value = ViewBag.LoanStatus })
@Html.HiddenFor(model => model.DateStamp, new{ @Value = ViewBag.DateTime })
And the html that is produced 和产生的html
<input Value="55" data-val="true" data-val-number="The field ID must be a number." data-val-required="The ID field is required." id="ID" name="ID" type="hidden" value="" />
<input Value="1" data-val="true" data-val-number="The field Reason must be a number." id="Reason" name="Reason" type="hidden" value="" />
<input Value="1" data-val="true" data-val-number="The field PoolType must be a number." id="PoolType" name="PoolType" type="hidden" value="" />
<input Value="Not Complete" id="LoanStatus" name="LoanStatus" type="hidden" value="" />
<input Value="2012-12-12 10:00.46" data-val="true" data-val-required="The DateStamp field is required." id="DateStamp" name="DateStamp" type="hidden" value="" />
EDIT Code without Html helper Line from .cshtml file <input type="hidden" name="LoanStatus" id="LoanStatus" data-val="true" value="@ViewBag.LoanStatus" />
.cshtml文件中没有HTML帮助程序行的EDIT代码
<input type="hidden" name="LoanStatus" id="LoanStatus" data-val="true" value="@ViewBag.LoanStatus" />
Line from html source <input type="hidden" name="LoanStatus" id="LoanStatus" data-val="true" value="Not Complete" />
来自html源的行
<input type="hidden" name="LoanStatus" id="LoanStatus" data-val="true" value="Not Complete" />
When using @Html.HiddenFor
the value for the input is read from your model object. 使用
@Html.HiddenFor
,将从模型对象中读取输入值。 You try to overwrite it by specifying a value for the Value
attribute. 您尝试通过为
Value
属性指定一个值来覆盖它。 I guess this won't work and as you see, you get empty value attributes. 我想这是行不通的,而且如您所见,您会得到空值属性。
To solve your problem, don't use the viewbag but pass a model object with the values set to your view like this: 要解决您的问题,请不要使用viewbag,而是将具有设置值的模型对象传递给视图,如下所示:
Controller: 控制器:
return View(new YourModel { LoanStatus = "Not Complete" });
View: 视图:
@Html.HiddenFor(m => m.LoanStatus)
Instead of using the Html helper here, why not try just making the line manually as follows: 而不是在这里使用Html帮助程序,为什么不尝试按如下所示手动进行生产:
<input type="hidden" name="LoanStatus" value="@ViewBag.LoanStatus"/>
I still don't know why each of your lines has the value set in 2 places. 我仍然不知道为什么您的每一行都有在2个地方设置的值。 Could you post more code?
您可以发布更多代码吗?
EDIT: I think that the lowercase value is being passed back into your controller. 编辑:我认为小写的值被传递回您的控制器。 Try using @value (lowercase v) instead and see if that fixes it.
尝试改用@value(小写字母v),看看是否可以解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.