2011-10-20 21 views
1

私はフォームを持っています。私のフォームでは、ユーザーは自分の詳細を記入し、チェックボックスにある興味のあるものを選択できます。私は部分的なビューとしてフォームに興味のセクションを配置します。mvc3フォームのチェックボックスで選択した値を取得する方法は?

フォーム誕生

  • 場所を持つ、

    1. 名前
    2. 興味(チェックボックスリスト)

    私は、すべてのフィールドの名前、誕生日を持つモデルを持っています場所。 そしてLookingFormodelのもう一つのモデル。

    今、私はフォームを提出します。名前、誕生日、パースのようなすべてのフィールドがモデル化されていますが、チェックボックスの選択項目リストが表示されません。

    フォーム送信時にチェックボックスの値を取得するにはどうすればよいですか?

  • +1

    いくつかのコードを表示できますか? ASP.NET MVC 3はフォームフィールドを自動的にモデルにマップすることができます。関心リストに関するすべてのものなので、ビューとモデルの適切な部分を表示してください。 – Rhapsody

    答えて

    4

    これはエディタテンプレートの候補のようです。いつものように私たちは、ビューモデルを設計することにより起動します。その後、

    public class MyViewModel 
    { 
        public string Name { get; set; } 
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] 
        public DateTime? DateOfBirth { get; set; } 
        public string Place { get; set; } 
        public IEnumerable<InterestViewModel> Interests { get; set; } 
    } 
    
    public class InterestViewModel 
    { 
        public int Id { get; set; } 
        public string InterestLabel { get; set; } 
        public bool IsSelected { get; set; } 
    } 
    

    コントローラ:

    public class HomeController : Controller 
    { 
        public ActionResult Index() 
        { 
         var model = new MyViewModel 
         { 
          Name = "john", 
          DateOfBirth = new DateTime(1990, 1, 1), 
          Place = "Spain", 
          Interests = new[] 
          { 
           new InterestViewModel { Id = 1, InterestLabel = "cinema" }, 
           new InterestViewModel { Id = 2, InterestLabel = "sport" }, 
           new InterestViewModel { Id = 3, InterestLabel = "books" }, 
          } 
         }; 
         return View(model); 
        } 
    
        [HttpPost] 
        public ActionResult Index(MyViewModel model) 
        { 
         // TODO: process the results here, the view model will be 
         // correctly bound 
         .... 
        } 
    } 
    

    その後、ビュー(~/Views/Home/Index.cshtml

    @model MyViewModel 
    
    @using (Html.BeginForm()) 
    { 
        <div> 
         @Html.LabelFor(x => x.Name) 
         @Html.EditorFor(x => x.Name) 
        </div> 
        <div> 
         @Html.LabelFor(x => x.DateOfBirth) 
         @Html.EditorFor(x => x.DateOfBirth) 
        </div> 
        <div> 
         @Html.LabelFor(x => x.Place) 
         @Html.EditorFor(x => x.Place) 
        </div> 
        <h2>Interests</h2> 
        @Html.EditorFor(x => x.Interests) 
    
        <button type="submit">OK</button> 
    } 
    

    とレンダリングされる、対応するエディタテンプレートインタレストコレクションの各要素(~/Views/Home/EditorTemplates/InterestViewModel.cshtml):

    @model InterestViewModel 
    
    @Html.LabelFor(x => x.IsSelected, Model.InterestLabel) 
    @Html.CheckBoxFor(x => x.IsSelected) 
    @Html.HiddenFor(x => x.Id) 
    @Html.HiddenFor(x => x.InterestLabel) 
    
    +0

    こんにちは、あなたは 'InterestViewModel.cshtml'を' Index.cshtml'にリンクしています。私はあなたの言ったことにちょうど従っていますが、チェックボックスの代わりに値を取得しています。 – FosterZ

    +0

    これは慣例により動作します:ビューモデルの中で、 'Interests'プロパティは' IEnumerable '=> asp.net mvcが'/Views/Shared/EditorTemplates/InterestViewModel.cshtml'ファイルを検索します。コレクションで使用される型名。 –

    +0

    ええ、丁度ありがとうございます..ちょうど前に、私は 'EditorTemplates'について私はこれを認識していなかったsearch'd .. – FosterZ

    関連する問題