Asp.net mvc 3 未在视图中设置默认表单字段值
我试图用默认值填充输入文本字段,但当视图呈现时,默认值不会应用于输入文本字段 重要细节 我正在将值从控制器操作传递到ViewBag中 在查看页面上调试时,我看到ViewBag具有我需要的默认值 有人能告诉我是否在视图的输入文本字段中正确设置了默认值吗 谢谢 这是我试图填充的输入文本框:Asp.net mvc 3 未在视图中设置默认表单字段值,asp.net-mvc-3,viewbag,Asp.net Mvc 3,Viewbag,我试图用默认值填充输入文本字段,但当视图呈现时,默认值不会应用于输入文本字段 重要细节 我正在将值从控制器操作传递到ViewBag中 在查看页面上调试时,我看到ViewBag具有我需要的默认值 有人能告诉我是否在视图的输入文本字段中正确设置了默认值吗 谢谢 这是我试图填充的输入文本框: <div class="editor-field"> @Html.EditorFor(model => model.SearchRadius, new { @Value = V
<div class="editor-field">
@Html.EditorFor(model => model.SearchRadius, new { @Value = ViewBag.SearchRadius})
</div>
@EditorFor(model=>model.SearchRadius,新的{@Value=ViewBag.SearchRadius})
请您试一试好吗
<div class="editor-field">
@Html.TextBoxFor(model => model.SearchRadius, new { @Value = ViewBag.SearchRadius})
</div>
@Html.TextBoxFor(model=>model.SearchRadius,新的{@Value=ViewBag.SearchRadius})
请注意@Value中的'V';这是首都。由于某种原因,@value-dosent似乎起作用了您能试试吗
<div class="editor-field">
@Html.TextBoxFor(model => model.SearchRadius, new { @Value = ViewBag.SearchRadius})
</div>
@Html.TextBoxFor(model=>model.SearchRadius,新的{@Value=ViewBag.SearchRadius})
请注意@Value中的'V';这是首都。出于某种原因,@value dosent似乎可以工作如果您将EditorFor更改为TextBoxFor,它应该可以工作,因为EditorFor在您尝试的内容上没有任何重载但是我建议在控制器操作中设置
model.SearchRadius
的默认值,而不是设置一个dupe viewbag项目:
控制器动作:
public ActionResult Index() {
var model = new MyViewModel();
model.SearchRadius = "Whatever";
return View();
}
<div class="editor-field">
@Html.EditorFor(model => model.SearchRadius)
</div>
剃须刀:
public ActionResult Index() {
var model = new MyViewModel();
model.SearchRadius = "Whatever";
return View();
}
<div class="editor-field">
@Html.EditorFor(model => model.SearchRadius)
</div>
@EditorFor(model=>model.SearchRadius)
如果您将EditorFor更改为TextBoxFor,它应该可以工作,因为EditorFor在您尝试的内容上没有任何重载但是我建议在控制器操作中设置model.SearchRadius
的默认值,而不是设置一个dupe viewbag项目:
控制器动作:
public ActionResult Index() {
var model = new MyViewModel();
model.SearchRadius = "Whatever";
return View();
}
<div class="editor-field">
@Html.EditorFor(model => model.SearchRadius)
</div>
剃须刀:
public ActionResult Index() {
var model = new MyViewModel();
model.SearchRadius = "Whatever";
return View();
}
<div class="editor-field">
@Html.EditorFor(model => model.SearchRadius)
</div>
@EditorFor(model=>model.SearchRadius)
EditorFor没有接受html属性的重载。将SearchRadius设置为控制器中所需的值有什么问题?EditorFor没有接受html属性的重载。将SearchRadius设置为控制器中所需的值有什么问题?