[英]mvc 4 asp.net c# passing boolean value from controller to view
How can i get Boolean value or the true or false from controller to a Checkbox in View using this:我怎样才能使用这个从控制器获取布尔值或真或假到视图中的复选框:
@Html.CheckBox("condition", @ViewData["Condition"])
or或者
<input type = "checkbox" checked = "**true or false?**">
I have this in my Controller..我的控制器中有这个..
public ActionResult Member(string sortOrder, string filter, string searchString, int? page, **bool? condition = false**)
and in my view is like this在我看来是这样的
please help me for this...请帮我这个...
update your controller更新您的控制器
public ActionResult Member(string sortOrder, string filter, string searchString, int? page,bool condition)
{
ViewData["Condition"] = condition;
// code line 1
// code line 2
...
}
If you are looking for checkbox check/uncheck code for Razor view如果您正在寻找 Razor 视图的复选框选中/取消选中代码
@Html.EditorFor(x => x.condition)
Will generate:会产生:
<input id="condition" type="checkbox" value="true" name="condition" />
<input type="hidden" value="false" name="condition" />
How does it work:它是如何工作的:
If checkbox not checked, form submit only hidden (false) If checked, then form submit two fields (false and true) and MVC set true for bool property如果未选中复选框,则表单仅提交隐藏(假)如果选中,则表单提交两个字段(假和真),MVC 为 bool 属性设置真
<input id="condition" name="condition" type="checkbox" value="@ViewData["Condition"]" />
This will always send default value, if checked如果选中,这将始终发送默认值
reference : https://stackoverflow.com/a/14731571/2318852参考: https : //stackoverflow.com/a/14731571/2318852
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.