2011-07-19 34 views

答えて

3

あなたの解決策は良いです、それは動作するはずです。しかし、そうではないので、私はあなたのビューにnullモデルを渡していると思います - この場合は、場所、そのnullを評価することは決してありません。非nullモデルをビューに渡してみてください。

return View(new LocationViewModel()); 
0

次のソースを使用してソリューションを試してみると、問題なく機能します。

モデル

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> 
関連する問題