在 ASP.NET mvc 中设置和获取复选框的默认值
Set and Get the default value of checkbox in ASP.NET mvc
我在 ASP.Net MVC 项目中使用 Checkbox,
我可以将复选框设置为默认不选中。
Age:
<input class="associationCheckbox" type="checkbox" name="age" />
当我 post 表单(因为只有复选框)时,复选框没有显示在 formCollection 中(这里预期的名称和值是年龄,'false')。
使用下面的代码,我正在读取值。
[HttpPost]
public ActionResult Index(FormCollection formCollection)
{
foreach (var key in formCollection.Keys)
{
var value = formCollection[key.ToString()];
}
return RedirectToAction("CorticonIndex", "Test");
}
如果我在 运行 时选中复选框,则提交表单。
我可以得到名称和值。
如果我不勾选复选框,如何获取默认值(false)?
我尝试使用模型 属性 设置值 属性,但出现了同样的问题。
型号Class:
public bool CheckBoxvalue { get; set; }
this.CheckBoxvalue=false
Age: <input class="associationCheckbox" type="checkbox" name="age" value=@Model.CheckBoxvalue />
谁能建议我如何设置和获取默认值。
我从堆栈溢出中查看了下面 post,但没有满足我的要求的信息。
How to set a CheckBox by default Checked in ASp.Net MVC
http://www.tutorialsteacher.com/mvc/htmlhelper-checkbox-checkboxfor
谢谢,
巴鲁
你可以这样做:
Age: <input class="associationCheckbox" type="checkbox" name="age" @(Model.CheckBoxvalue ? "checked" : "") value=@Model.CheckBoxvalue />
但是如果您使用的是 razor,那么使用 HTMLHelper 方法总是好的
我在 ASP.Net MVC 项目中使用 Checkbox,
我可以将复选框设置为默认不选中。
Age:
<input class="associationCheckbox" type="checkbox" name="age" />
当我 post 表单(因为只有复选框)时,复选框没有显示在 formCollection 中(这里预期的名称和值是年龄,'false')。
使用下面的代码,我正在读取值。
[HttpPost]
public ActionResult Index(FormCollection formCollection)
{
foreach (var key in formCollection.Keys)
{
var value = formCollection[key.ToString()];
}
return RedirectToAction("CorticonIndex", "Test");
}
如果我在 运行 时选中复选框,则提交表单。 我可以得到名称和值。
如果我不勾选复选框,如何获取默认值(false)?
我尝试使用模型 属性 设置值 属性,但出现了同样的问题。
型号Class:
public bool CheckBoxvalue { get; set; }
this.CheckBoxvalue=false
Age: <input class="associationCheckbox" type="checkbox" name="age" value=@Model.CheckBoxvalue />
谁能建议我如何设置和获取默认值。
我从堆栈溢出中查看了下面 post,但没有满足我的要求的信息。
How to set a CheckBox by default Checked in ASp.Net MVC
http://www.tutorialsteacher.com/mvc/htmlhelper-checkbox-checkboxfor
谢谢, 巴鲁
你可以这样做:
Age: <input class="associationCheckbox" type="checkbox" name="age" @(Model.CheckBoxvalue ? "checked" : "") value=@Model.CheckBoxvalue />
但是如果您使用的是 razor,那么使用 HTMLHelper 方法总是好的