在模型中:
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,
}
在 Controller - 索引ActionResult中,我返回了我的模型并将其渲染到 View 页面中,如下所示:
@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"});
}
最佳答案
这里有一些不一致的地方。您正在为每行渲染 2 个单选按钮,因此您可能希望将 View 模型重新组织为:
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; }
}
然后:
@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" });
}
或者,如果您想要单一付款方式,您可以保持 View 模型不变,但然后将单选按钮保留在 View 中的循环之外。像这样:
@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" />
}
关于asp.net-mvc-3 - 如何在mvc 3中获取标签值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9747629/