为什么@Html.LabelFor(model => model.data, ((string)ViewBag.id))
以public ActionResult Index(Data item)
null而不是100
返回。如何解决这个问题?
public ActionResult Index()
{
ViewBag.id = "100";
return View();
}
[HttpPost]
public ActionResult Index(Data item)
{
return View();
}
Index.cshtml
@model TestForm.Models.Data
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using(@Html.BeginForm())
{
@Html.LabelFor(model => model.data, ((string)ViewBag.id))
<input type="submit"/>
}
答案 0 :(得分:0)
为什么@ Html.LabelFor(model =&gt; model.data,((string)ViewBag.id))在公共ActionResult索引(数据项)中返回null而不是100
您永远不会在该方法中设置值。如果您希望它在该方法之后显示在标签中,则需要将ViewBag.id = "100";
添加到方法中。
如果您尝试将该设置设为默认值,那么您实际上是在错误地创建表单。它应该是<%= Html.TextBoxFor(model => model.[fieldname], ViewBag.id))%>
然后将其发回您的Data item
对象并进行设置。注意,您必须更改[fieldname]