Reputation: 3596
i tried to add , new { @Value = "test" } but failed.
then i modify the model to
private string _location = string.Empty;
public string location { get { return _location; } set { _location = value; } }
or even change the string.empty into my default text, still failed.
any idea ? Thx
Upvotes: 0
Views: 7918
Reputation: 60468
tryed your solution with the following source and it works fine for me.
Model
public class LocationViewModel
{
private string _location = "test";
public string Location
{
get { return _location; }
set { _location = value; }
}
}
Action
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View(new LocationViewModel());
}
View
@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>
Upvotes: 0
Reputation: 39501
Your solution is good, it should work. But as it does not, i think you are passing null model to your view - in this case it never gets to evaluating location, its null. Try passing non null model to your view.
return View(new LocationViewModel());
Upvotes: 3