Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 如何在ASP.NET MVC中将字符串类型的模型属性呈现为复选框_Asp.net Mvc_Asp.net Mvc 3_Asp.net Mvc 2 - Fatal编程技术网

Asp.net mvc 如何在ASP.NET MVC中将字符串类型的模型属性呈现为复选框

Asp.net mvc 如何在ASP.NET MVC中将字符串类型的模型属性呈现为复选框,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-2,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc 2,我想在MVC视图中将字符串类型显示为复选框,但在HTTP post上将其返回为字符串类型。问题是它在HTTP Post上返回false。下面是我的代码: 视图: 控制器: public ActionResult Index() { var cars = new List<Car>(){ new Car(){IsFourWheel = "true"},new Car(){IsFourWheel = "false"} };

我想在MVC视图中将字符串类型显示为复选框,但在HTTP post上将其返回为字符串类型。问题是它在HTTP Post上返回false。下面是我的代码:

视图:

控制器:

 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);
        }
public ActionResult Index()
{
var cars=new List(){new Car(){IsFourWheel=“true”},new Car(){IsFourWheel=“false”};
返回视图(汽车);
}
[HttpPost]
公共行动结果索引(列出车辆)**选择true时,问题是Fourwheel为false**
{           
返回视图(汽车);
}

非常感谢您提出的任何理想方案。

您可以如下更改您的viewmodel:

public class Car
    {
        public string IsFourWheel { get; set; }
        public bool IsFourWheelBool { get { return bool.Parse(IsFourWheel); } }
    }
@Html.EditFor(x => x.IsFourWheelBool);
您的视图如下所示:

public class Car
    {
        public string IsFourWheel { get; set; }
        public bool IsFourWheelBool { get { return bool.Parse(IsFourWheel); } }
    }
@Html.EditFor(x => x.IsFourWheelBool);

我想如果你给你的模型加上一个Id会更容易。就这样
型号:

public class Car
{
    public int CarID { get; set; }
    public string IsFourWheel { get; set; }        
}

查看:

@model IEnumerable<Car>
foreach (var car in Model)
{
    if(car.IsFourWheel == "true"){
        <input type="checkbox" name="carID" value="@car.CarID" checked="checked" />
    }
    else
    {
        <input type="checkbox" name="carID" value="@car.CarID" />
    }
}
@model IEnumerable
foreach(模型中的var车辆)
{
如果(car.IsFourWheel==“true”){
}
其他的
{
}
}
控制器:

[HttpPost]
public ActionResult Index(List<int> carID)
{
    //handle selected cars here
    return View();
}
[HttpPost]
公共行动结果索引(列表carID)
{
//在这里处理选定的车辆
返回视图();
}

您可以尝试在助手中指定模板名称:

@Html.EditorFor(car => car.IsFourWheel, "CheckBox")
并在
~/Views/{YourControllerName}/EditorTemplates/CheckBox.cshtml
~/Views/Shared/EditorTemplates/CheckBox.cshtml>中定义模板以您想要的方式呈现数据

您可以在此处找到Brad Wilson关于MVC模板的一系列文章:

它适用于MVC2,但大多数概念仍然适用于MVC3(除了Razor语法)

更新:

实际上,您可能不需要为此定制模板。尝试改用
@Html.CheckBoxFor(car=>car.IsFourWheel)

更新2:

将以下模板放入
~/Views/Shared/EditorTemplates

IsFourWheel.cshtml

@functions {
    private bool IsChecked() {
        if (ViewData.Model == null) return false;
        return Convert.ToBoolean(ViewData.Model, System.Globalization.CultureInfo.InvariantCulture);
    }
}

@Html.CheckBox("", IsChecked(), new { @class = "check-box" })
然后从你的角度,这样称呼它:

@Html.EditorFor(model => model.IsFourWheel, "IsFourWheel")

我对它进行了测试,绑定在GET和POST场景中都能正常工作。

谢谢您的建议。我更新了我的问题。我有一批汽车,需要邮寄。您当前的解决方案仅适用于一辆车。当存在一组车时,输入标记需要一个id/名称,以允许模型绑定器分配值。这需要更改域模型,这不是理想的方案。在视图中使用域模型通常被认为是糟糕的设计。()最好创建视图模型以方便查看。使用像automapper()这样的工具,可以很容易地从域模型创建viewmodels。但是手动创建viewmodels也是非常可行的。谢谢。我知道。我的问题只是一个简单的例子。谢谢。请详细说明如何在CheckBox.cshtml上实现它。例如,您可以查看MVC源代码中布尔字段的默认模板(what CheckBoxFor renders),但正如我所说的,除非您对
IsFourWheels
属性做了非常具体的操作,您可能不需要模板,只需使用
@Html.CheckBoxFor(car=>car.IsFourWheel)
即可。Daniel Liuzzi,@Html.CheckBoxFor需要bool类型。@Pingpong是的,我的错。我用模板更新了答案,对于以字符串形式存储的布尔值,该模板可以正常工作。谢谢。我将使用它,看看它是否与我的实现一起工作。