C# 将复选框值传递给控制器
如何将值从复选框(视图)传递到控制器C# 将复选框值传递给控制器,c#,model-view-controller,C#,Model View Controller,如何将值从复选框(视图)传递到控制器 <div className="row"> <div class="filters-container col-sm-3"> <h5 class="navbar navbar-dark bg-info">Brands</h5> <p>Apple</p> <input type="checkbox" class="filter
<div className="row">
<div class="filters-container col-sm-3">
<h5 class="navbar navbar-dark bg-info">Brands</h5>
<p>Apple</p>
<input type="checkbox" class="filter" id="AppleFilter" value="true" name="Filters"></input>
</div>
</div>
品牌
苹果
我的控制器:
public class ProductController : DaoController<ProductDao, Product>
{
[HttpGet]
public override IActionResult Get()
{
return InnerGet();
}
[HttpGet("{value}")]
public IActionResult Search(string value)
{
var daoManager = HttpContext.RequestServices.GetService<DaoManager>();
List<Product> products = daoManager.ProductDao.SearchProduct(value);
return Ok(products);
}
[HttpGet]
public IActionResult Filter(string responsables, bool checkResp = false)
{
///????
}
公共类ProductController:DaoController
{
[HttpGet]
公共覆盖IActionResult Get()
{
返回InnerGet();
}
[HttpGet(“{value}”)]
公共IActionResult搜索(字符串值)
{
var daoManager=HttpContext.RequestServices.GetService();
List products=daoManager.ProductDao.SearchProduct(值);
退货确认(产品);
}
[HttpGet]
公共IActionResult筛选器(字符串响应,bool checkResp=false)
{
///????
}
我不知道在视图和控制器中放置什么来传递值。您应该引入一个ViewModel。发送值的一个简单方法是将此ViewModel发布到控制器操作
public class FilterViewModel {
[DisplayName("Apple")]
public bool DoApplyAppleFilter { get; set; }
}
告诉视图Filter.cshtml
使用此ViewModel:
@model FilterViewModel
@* this <form> will be posted to the "Filter" action in the "ProductController" *@
@using (Html.BeginForm("Filter", "Product", FormMethod.Post, new { @class = "ym-form"})) {
@Html.LabelFor(m => m.DoApplyAppleFilter) @* renders <label> with the given DisplayName for the checkbox *@
@Html.CheckBoxFor(m => m.DoApplyAppleFilter) @* renders <input type="checkbox"> *@
<button type="submit">Apply filter</button>
}
除了提交
,您还可以使用AJAX发布
,或者提取值并使用GET请求。您应该引入ViewModel。发送值的简单方法是将此ViewModel发布到控制器操作
public class FilterViewModel {
[DisplayName("Apple")]
public bool DoApplyAppleFilter { get; set; }
}
告诉视图Filter.cshtml
使用此ViewModel:
@model FilterViewModel
@* this <form> will be posted to the "Filter" action in the "ProductController" *@
@using (Html.BeginForm("Filter", "Product", FormMethod.Post, new { @class = "ym-form"})) {
@Html.LabelFor(m => m.DoApplyAppleFilter) @* renders <label> with the given DisplayName for the checkbox *@
@Html.CheckBoxFor(m => m.DoApplyAppleFilter) @* renders <input type="checkbox"> *@
<button type="submit">Apply filter</button>
}
除了提交
,您还可以使用AJAX发布
,或者提取值并使用GET请求。您的复选框必须是某个表单的子元素。然后,选中状态将是表单变量之一。您的复选框必须是某个表单的子元素。然后,选中状态将是您的for之一m个变量。