Asp.net mvc 3 如何在MVC3中获取标签值?
在型号中:Asp.net mvc 3 如何在MVC3中获取标签值?,asp.net-mvc-3,razor,Asp.net Mvc 3,Razor,在型号中: 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 { g
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,
}
这里有些矛盾。您正在为每行渲染两个单选按钮,因此可能需要将视图模型重新组织为:
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; }
}
或者,如果您想要一种单一的付款方式,您可以保持视图模型的原样,但将单选按钮保留在视图的循环之外。就像这样:
@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" />
}
@使用(Html.BeginForm(“保存”、“页面”、FormMethod.Post、新的{id=“保存”}))
{
对于(var i=0;ix.Rows[i].ID)
@LabelFor(x=>x.Rows[i].问题)
@EditorFor(x=>x.Rows[i].问题)
}
@Html.标签(“支付现金”、“现金”)
@RadioButtonOn(x=>x.PaiementMethod,“现金”,新的{id=“付款\现金”})
@Html.标签(“付款抄送”、“信用卡”)
@RadioButton(x=>x.PaiementMethod,“信用卡”,新的{id=“payment\u cc”})
}
RowsCollection代码中不包含id您知道吗?我还将删除单选按钮中的html名称属性,该属性有时会给我的项目中的内置默认模型绑定带来一些问题。我添加了这个。这里只是我没有定义。任何html页面完美呈现的方式都是我在.cshtml(razor)中编写的
。但我的疑问是,当提交操作时,我如何才能进入控制器
?
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" });
}
@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" />
}