2012-02-21 3 views
0

フォーム上の私のモデルで、化合物Addressの通り,都市などを含む)プロパティを設定するにはどうすればよいですか?複合プロパティをフォームに設定する。 ASP.NET MVC

これは何らかのヘルパーであるべきですか?

ビュー

@model Person 

@using(@Html.BeginForm("Create", "Person", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    @Html.ValidationSummary()  
    <fieldset> 
     <p> 
      @Html.Label("MiddleName") <br /> 
      @Html.TextBoxFor(p => p.MiddleName) 
     <p> 

     @* And so on... *@ 

     @* What about Model.Address ? How do I assign person's `Address` properties? *@ 

    </fieldset> 
} 

ありがとう!

答えて

0

モデル:

public class Address 
{ 
    public string Street { get; set; } 
    public string City { get; set; } 
} 

public class MyViewModel 
{ 
    public Address Address { get; set; } 
} 

コントローラー:

public class HomeController: Controller 
{ 
    public ActionResult Index() 
    { 
     var model = new MyViewModel(); 
     return View(model); 
    } 

    [HttpPost] 
    public ActionResult Index(MyViewModel model) 
    { 
     // the default model binder will take care of populating 
     // your view model along with its complex properties 
     ... 
    } 

} 

ビュー:

@model MyViewModel 
@using (Html.BeginForm()) 
{ 
    <div> 
     @Html.LabelFor(x => x.Address.Street) 
     @Html.EditorFor(x => x.Address.Street) 
    </div> 
    <div> 
     @Html.LabelFor(x => x.Address.City) 
     @Html.EditorFor(x => x.Address.City) 
    </div> 

    <p><button type="submit">OK</button></p> 
} 
+0

[OK]をクリックします。私はそれを断言する。しかし、私は強く型付けされた*ビュー( 'Person'による)を使い、そのモデル(アトミック)プロパティに割り当てようとしています。 'Address'はアトミックではありません。どうすれば同じ形でこれをやりますか?私は確信していないが、おそらくそれはビュー上でいくつかのモデルを使用するためにサポートされていないのですか? – lexeme

+0

@helicera、あなたが言ったことは分かりません。たぶんあなたはコードの問題を実例にすることができますか? –

+0

私の編集を見てください。 – lexeme

関連する問題