[英]pass checkbox value to controller
How do I pass a value from my checkbox (view) to the 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" id="AppleFilter" value="true" name="Filters"></input>
</div>
</div>
My controller: 我的控制器:
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)
{
///????
}
I dont know what to put in the view and controller to pass the values. 我不知道在视图和控制器中放置什么来传递值。
You should introduce a ViewModel. 您应该引入一个ViewModel。 A simple way to send the values is to POST this ViewModel to your controller action.
发送值的一种简单方法是将此ViewModel张贴到您的控制器操作中。
public class FilterViewModel {
[DisplayName("Apple")]
public bool DoApplyAppleFilter { get; set; }
}
Tell the View Filter.cshtml
to use this ViewModel: 告诉View
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>
}
Controller Action: 控制器动作:
[HttpPost]
public IActionResult Filter(FilterViewModel viewModel) {
bool isChecked = viewModel.DoApplyAppleFilter;
// ...
}
Instead of a submit <button>
, you can also use AJAX to POST the <form>
, or extract the values and use a GET request. 除了提交
<button>
,还可以使用AJAX来发布<form>
,或者提取值并使用GET请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.