我在MVC中有一个强类型的单选按钮。如何将值保存到模型属性
<ul class="formlist">
<li class="width100">
<div class="form-check form-check-inline">
<label class="form-check-label">
@Html.RadioButtonFor(model => model._Account[i].account_flag, 1, new { @id = Model._Service[i].service_id, @Name = "text" + @Model._Service[i].service_id, @checked = "false" })<span>Yes</span>
</label>
</div>
<div class="form-check form-check-inline">
<label class="form-check-label">
@Html.RadioButtonFor(model => model._Account[i].account_flag, 0, new { @id = Model._Service[i].service_id, @Name = "text" + @Model._Service[i].service_id, @checked = "checked" })<span>No</span>
</label>
</div>
</li>
</ul>
答案 0 :(得分:0)
这段代码对我来说很好。希望这会有用。
查看:
@using (Html.BeginForm("Test", "Home", FormMethod.Post, new { @class = "my_form" }))
{
<ul class="formlist">
<li class="width100">
<div class="form-check form-check-inline">
<label class="form-check-label">
@Html.RadioButtonFor(model => model._Account[i].account_flag, 1, new { id = Model._Service[i].service_id, Name = "text" + @Model._Service[i].service_id, @checked = "false" })<span>Yes</span>
</label>
</div>
<div class="form-check form-check-inline">
<label class="form-check-label">
@Html.RadioButtonFor(model => model._Account[i].account_flag, 0, new { id = Model._Service[i].service_id, Name = "text" + @Model._Service[i].service_id, @checked = "checked" })<span>No</span>
</label>
</div>
</li>
}
型号:
using System.Collections.Generic;
namespace sample_project.Models
{
public class SampleModel
{
public List<Account> _Account { get; set; }
}
public class Account
{
public string account_flag { get; set; }
public int service_id { get; set; }
}
}
控制器:
using sample_project.Models;
using System.Collections.Generic;
using System.Web.Mvc;
namespace sample_project.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
SampleModel model = new SampleModel();
model._Account = new List<Account>();
model._Account.Add(new Account() { service_id=1 });
return View(model);
}
[HttpPost]
public ActionResult Test(SampleModel model)
{
string account_flag = model._Account[0].account_flag;
return View();
}
}