【问题标题】:AJAX not transporting checkbox values to controllerAJAX 不将复选框值传输到控制器
【发布时间】:2021-09-23 17:58:43
【问题描述】:

我有一个模型如下:

public class SearchModel
{
    public List<CheckBoxResponse> Status { get; set; }
}

public class CheckBoxResponse
{
    public string ItemText { get; set; }
    public bool Selected { get; set; }
}

视图如下:

<div id="divSearchParams">
    <div class="col-xs-12 col-sm-12 col-md-5 col-lg-5">
            <div class="form-group">
                <label>Status</label> <br />
                @for (var i = 0; i < Model.Status.Count; i++)
                {                       
                    <input type="checkbox" asp-for="@Model.Status[i].Selected" />
                    <label asp-for="@Model.Status[i].Selected">@Model.Status[i].ItemText</label>
                    <input type="hidden" asp-for="@Model.Status[i].ItemText" />
                }
            </div>
        </div>
</div>

我的 AJAX 调用:

$(document).on("click", "#search", function () {

    var model = $('#divSearchParams').find("input,select,textarea").serialize();
       
    $.ajax({
    url: targetUrl,
    type: 'POST',
    data: { model: model },
    cache: true,
    async: true,
    }).done(function (result) {
   
    }).fail(function (error) {
        
    }).always(function () {
    
    }
});   
});

所以在序列化时,模型值为 "Status%5B0%5D.ItemText=Open&Status%5B1%5D.Selected=true"

但是,当我检查控制器中模型的值时,我看到模型中的状态值为 null。

public async Task<ActionResult> Students(SearchModel model)
{

}

我错过了什么?

【问题讨论】:

  • 如果您发布序列化到此的模型,那实际上不是 JSON。那是查询字符串参数。因此,它将无法将其解析为 JSON。您可以尝试将此查询字符串附加到 targetURL:targetUrl + "?" + 模型,看看是否可行,但这可能不是您想要的?
  • @The_Outsider:JQuery 'serialize()' 方法返回一个字符串。因此,如果您将Students() 参数类型从SearchModel 更改为string,您至少会看到您发送的内容。

标签: c# ajax asp.net-core


【解决方案1】:

Ajax post data 默认为 contentType application/x-www-form-urlencoded; charset=UTF-8,需要修改:

data: { model: model },

收件人:

data: model,

确保您的后端控制器不是 ApiController,否则您需要在 SearchModel 参数上指定特定的 [FromForm] 属性。

public class HomeController : Controller
{
    [HttpPost]
    public async Task<ActionResult> Students(SearchModel model)
    {
        //do your stuff...
    }
}

结果展示:

参考:

https://stackoverflow.com/a/65572281/11398810

更新:

如果您的操作包含第二个字符串参数:

[HttpPost]
public async Task<ActionResult> Students(SearchModel model,string Id)
{
        //....
}

在视图中添加一个具有name="Id" 的输入:

<input name="Id" />

如果您的操作包含第二个模型类型参数:

型号:

public class TestViewModel
{
    public int Id{ get; set; }
}

控制器:

[HttpPost]
public async Task<ActionResult> Students(SearchModel model, TestViewModel Item)
{
    //...
}

在视图中添加一个具有name="Item.Id" 的输入:

<input name="Item.Id" />

更新2:

控制器:

[HttpPost]
public async Task<ActionResult> Students(SearchModel model, string Id,bool isPartial)
{
    return View();
}

查看:

$(document).on("click", "#search", function () {
    var id = "aaa";
    var isPartial = false;
    var model = $('#divSearchParams').find("input,select,textarea").serialize();
    $.ajax({
        url: "/Home/Students?Id=" + id + "&isPartial=" + isPartial,
        type: 'POST',
        data: model,
        cache: true,
        async: true,
    }).done(function (result) {

    }).fail(function (error) {

    }).always(function () {

    });
}); 

【讨论】:

  • 这看起来不错,但是如果有第二个参数呢?在这种情况下我该如何传递第二个参数?
  • 同理。只要确保输入的名称属性与模型属性匹配。如果我的回答帮助您解决了您的问题,您能接受吗?请参考:How to accept as answer。谢谢。
  • 您可以编辑帖子并为多个参数提供示例吗?假设需要将模型和 id 发布到服务器?例如:public async Task Students(SearchModel model, string Id) {
  • 请检查我的更新答案。
  • 嗨@The_Outsider,您不能将{ model: model, isPartial: true, Id: Id },.serialize 数据结合起来发布到后端,如果其他参数没有在表单中,您可以将它们附加到url,例如:"/Home/Students?Id=" + id + "&amp;isPartial=" + isPartial。再次更新我的答案。
猜你喜欢
  • 2017-09-01
  • 2018-12-15
  • 2014-08-24
  • 1970-01-01
  • 1970-01-01
  • 2016-01-31
  • 1970-01-01
  • 2018-01-08
  • 2020-05-29
相关资源
最近更新 更多