【发布时间】:2011-08-26 10:49:07
【问题描述】:
我想在 MVC 视图上将字符串类型显示为复选框,但在 HTTP post 上将其作为字符串类型返回。问题是它在 HTTP Post 上返回 false。以下是我的代码:
查看:
@model List<Car>
foreach(var car in Model){
bool isFourWheel = false;
if(bool.TryParse(car.IsFourWheel, out isFourWheel){
@Html.CheckBox("IsFourWheel", isFourWheel); //need to be rendered as checkbox, but returns string type on HTTP POST
}
}
型号:
public class Car
{
public string IsFourWheel { get; set; } //bad naming, but it can contain any type, include boolean
}
控制器:
public ActionResult Index()
{
var cars = new List<Car>(){ new Car(){IsFourWheel = "true"},new Car(){IsFourWheel = "false"} };
return View(cars);
}
[HttpPost]
public ActionResult Index(List<Car> cars) **Problem IsFourWheel is false when true is selected **
{
return View(cars);
}
非常感谢任何理想。
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-2