首页 > 解决方案 > 在 asp.net.core c# 中通过 ajax 命令将布尔值发送到控制器

问题描述

我在视图部分有一个带有复选框类型输入的表单

<div class="checkbox">
<label class="">active</label>
<div class="icheckbox_flat-green checked" style="position: relative;">
<input type="checkbox" id="AdvActive" name="AdvActive" class="flat" checked="checked"/>
</div>                            
</div>   

并通过下面的ajax代码将值发送到控制器

$.ajax({
  type: "post",
  url: '@Url.Action("CreateAdvBanner", "Advertise")',
  cache: false,
   data: form.serialize()
})

除同一个复选框外,所有表单值都发送到控制器。当复选框被选中时,我想向控制器发送“True”吗?这是我的控制器

[HttpPost]
public IActionResult CreateAdvBanner(string AdvCode, int AdvPrice ,int AdvDay, string AdvDesc,bool AdvActive)
{

标签: javascriptc#htmlajaxmodel-view-controller

解决方案


将 JSON 发送到控制器

就像是

$.ajax({
  type: "post",
  url: '@Url.Action("CreateAdvBanner", "Advertise")',
  cache: false,
  data: {"AdvCode" : "itsValue", "AdvPrice":"itsValue", "AdvDay" : "itsValue", "AdvDesc" : "somevalue"}  
})

布尔值和数字不能在“”中


推荐阅读