2012-03-17 6 views
2

モデルでコントローラーmvc 3でラベル値を取得する方法は?

public class DataViewModel 
{ 
    public IList<RowsCollection> Rows { get; set; } 

    public PaiementMethod PaiementMethod { get; set; } 

} 
public class RowsCollection 
{ 
    public string ID { get; set; } 
    public string Question { get; set; } 
} 

public enum PaiementMethod 
{ 
    Cash, 
    CreditCard, 
} 

- インデックスのActionResult私は私のモデルを返し、そのビューにページをレンダリングしているようなもの:提出フォームアクションイベントで

 @model WebNexpo.Controllers.DataViewModel 
    @using (Html.BeginForm("Save", "page", FormMethod.Post, new { id = "saves"})) 
     { 
     foreach (var item in Model.Rows) 
     { 
      @Html.Label(item.Question) 
      <label for="paiement_cash"> 
      Cash</label> 
     @Html.RadioButtonFor(m => m.PaiementMethod, "Cash", new { name = "paiement_cash" + @item.ID + "", id = "radioID" + @item.ID + "", Group = "Group" + @item.ID + "" }) 
     <label for="paiement_cc"> 
      Credit card</label> 
     @Html.RadioButtonFor(m => m.PaiementMethod, "CreditCard", new { name = "paiement_cc" + @item.ID + "", id = "radioname" + @item.ID + "", Group = "Group" + @item.ID + "" }) 

     } 
     <input id="Saveser" type="submit" value="" class="button1" /> 
    } 

 [HttpPost] 
    public ActionResult Save(DataViewModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      //want to read all the label which selected rediobutton. 
       means suppose 4 question render on page and user have selected only 2 
      question of the answer.so How can accomplish here? 
     } 
     return RedirectToAction("Index", new {value = "123"}); 
    } 
+0

RowsCollectionにはコードが含まれていません。また、ラジオボタンのhtml name属性を削除して、組み込みのデフォルトモデルバインドにいくつかの問題を与えます。 – jacqijvv

+0

私のプロジェクトではこれを追加しました。ちょうど私は定義していません。私は '.cshtml(razor)'にコードを書いていますが、私の疑問はActionを提出するときに 'controller'にどうやって入るのですか? – Jigs

答えて

2

ここにはいくつかの矛盾があります。その後、

public class DataViewModel 
{ 
    public IList<RowsCollection> Rows { get; set; } 
} 

public class RowsCollection 
{ 
    public string ID { get; set; } 
    public string Question { get; set; } 
    public PaiementMethod PaiementMethod { get; set; } 
} 

と:あなたは、おそらくにあなたのビューモデルを再編成したいので、行ごとに、2つのラジオボタンをレンダリングしている

@model DataViewModel 
@using (Html.BeginForm("Save", "page", FormMethod.Post, new { id = "saves"})) 
{ 
    for (var i = 0; i < Model.Rows.Count; i++) 
    { 
     <div> 
      @Html.HiddenFor(x => x.Rows[i].ID) 
      @Html.LabelFor(x => x.Rows[i].Question) 
      @Html.EditorFor(x => x.Rows[i].Question) 

      @Html.Label("payment_cash" + i, "Cash") 
      @Html.RadioButtonFor(m => m.Rows[i].PaiementMethod, "Cash", new { id = "payment_cash" + i }) 

      @Html.Label("payment_cc" + i, "Credit card") 
      @Html.RadioButtonFor(m => m.Rows[i].PaiementMethod, "CreditCard", new { id = "payment_cc" + i }) 
     </div> 
    } 
    <input id="Saveser" type="submit" value="" class="button1" /> 
} 

し、最終的に:

[HttpPost] 
public ActionResult Save(DataViewModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     // model.Rows[0].PaiementMethod will contain the selected payment method for the first question 
     // model.Rows[1].PaiementMethod will contain the selected payment method for the second question 
     // ... 
    } 
    return RedirectToAction("Index", new { value = "123" }); 
} 

かもしあなたのビューモデルをそのままにして、ビューのループの外側にラジオボタンを残しておけば、1つの支払い方法が必要になります。同様に:

@using (Html.BeginForm("Save", "page", FormMethod.Post, new { id = "saves" })) 
{ 
    for (var i = 0; i < Model.Rows.Count; i++) 
    { 
     <div> 
      @Html.HiddenFor(x => x.Rows[i].ID) 
      @Html.LabelFor(x => x.Rows[i].Question) 
      @Html.EditorFor(x => x.Rows[i].Question) 
     </div> 
    } 

    @Html.Label("payment_cash", "Cash") 
    @Html.RadioButtonFor(x => x.PaiementMethod, "Cash", new { id = "payment_cash" }) 

    @Html.Label("payment_cc", "Credit card") 
    @Html.RadioButtonFor(x => x.PaiementMethod, "CreditCard", new { id = "payment_cc" }) 

    <input id="Saveser" type="submit" value="" class="button1" /> 
} 
関連する問題