我尝试添加,new { @Value = "test“},但失败了。
然后,我将模型修改为
private string _location = string.Empty;
public string location { get { return _location; } set { _location = value; } }或者甚至将string.empty更改为我的默认文本,仍然失败。
有什么想法吗?Thx
发布于 2011-07-19 16:52:06
你的解决方案很好,应该能行得通。但由于它没有,我认为您正在将null model传递给您的视图-在这种情况下,它永远不会得到评估位置,它的null。尝试将非空模型传递给视图。
return View(new LocationViewModel());发布于 2011-07-19 16:55:47
我用下面的源码尝试了你的解决方案,它对我来说很好。
模型
public class LocationViewModel
{
private string _location = "test";
public string Location
{
get { return _location; }
set { _location = value; }
}
}动作
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View(new LocationViewModel());
}视图
@model LocationViewModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Test</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Location)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Location)
@Html.ValidationMessageFor(model => model.Location)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>https://stackoverflow.com/questions/6744472
复制相似问题