简体   繁体   English

mvc 4 asp.net c# 将布尔值从控制器传递到视图

[英]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.

相关问题 c# asp.net core mvc 将数据从视图传递到控制器并从控制器返回到视图 - c# asp.net core mvc passing data from view to controller and from controller back to view ASP.NET MVC C#:将视图数据传递给 controller - ASP.NET MVC C# : passing view data to controller ASP.NET MVC 将值从控制器上的变量传递到视图 - ASP.NET MVC passing a value from a variable on a controller to a view ASP.NET MVC-使用C#将引用不同程序集的模型从控制器传递到视图 - ASP.NET MVC - Passing a Model referencing a different assembly from a Controller to a View using C# 将 javascript 数组从视图传递到 ASP.Net MVC 5 中的控制器 - Passing javascript array from view to controller in ASP.Net MVC 5 将数据从控制器传递到ASP.NET MVC中的视图 - Passing data from a controller to the view in ASP.NET MVC 将复杂的数组从Controller传递到View ASP.NET MVC - Passing complex array from Controller to View ASP.NET MVC 在ASP.NET MVC中将数据从视图传递到控制器失败 - Passing Data from View to Controller failed in asp.net mvc ASP.NET MVC 3 Razor:将数据从视图传递到控制器 - ASP.NET MVC 3 Razor: Passing Data from View to Controller 在ASP.NET MVC中从View传递模型到Controller - Passing Model to Controller from View in ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM