我正在尝试显示一个复选框列表并将选中的值保存到MVC 2中的数据库,但到目前为止我无法在视图上显示复选框列表。下面是我的代码。
视图模型:
public class Address
{
public int addressID { get; set; }
public string address { get; set; }
}
public class Client
{
public int ClientID { get; set; }
public string Name { get; set; }
public IEnumerable<Client> ClientOrdersGrid { get; set; }
public List<Address> Addresses { get; set; }
public List<int> SelectedAddressIDs { get; set; }
}
控制器:
public ActionResult Index()
{
JqGridClientRepository rep = new JqGridClientRepository();
Address ad=new Address() {address="abcd",addressID=1};
Address ad1=new Address() {address="kandy",addressID=2};
Address ad2=new Address() {address="colombo",addressID=3};
List<Address> lstAd=new List<Address>(){
ad,ad1,ad2
};
var model = new Client()
{
Addresses = lstAd,
ClientOrdersGrid=rep.GetClients()//This method gets an IEnumerable list of all clients
};
return View(model);
}
查看:
<% foreach (var item in Model.Addresses) { %>
// I want to display checkbox list of addresses here
<% } %>
答案 0 :(得分:0)
我修改了模型中的某些字段,如
public class Address
{
public int addressID { get; set; }
public string address { get; set; }
public bool AddreChecked { get; set; }
}
并在视图中更改了一些代码行
@foreach (var item in Model.Addresses)
{
<div> </div>
@Html.CheckBox("checkboxGroup", item.AddreChecked); @item.address
}
希望这有帮助!