是否可以使用 jquery ajax 将不同的模型类型传递给控制器​​?

Is it possible to pass different model type to controller using jquery ajax?

假设我有一个带有子论坛的论坛模型 属性。

查看:

@model Forum

@using (@Html.BeginForm("DoSomething", "Forum", FormMethod.Post, new { id= "SubForumForm" }))
{
     @Html.TextBoxFor(model => model.subForum.test1)
     @Html.TextBoxFor(model => model.subForum.test2)
     <input id="btn" type="button" value="CLICK" />
}

型号:

public class Forum
{
     public subForum SubForum { get; set; }
)

控制器:

[HttpPost]
public ActionResult DoSomething(model SubForum)
{
     if (ModelState.isValid)
     {
          //do something with the model
     }
}

我想通过 jquery ajax 将 SubForum 属性 传递给我的控制器 :

   $('#btn').click(function () {
        if($('#SubForumForm').valid()) {
            $.ajax({
                type: "POST",
                url: BASE_URL + "Forum/DoSomething/",
                dataType: 'json',
                data: $('#SubForumForm').serializeArray(),
                beforeSend: function (xhr) {
                },
                success: function (data) {
                },
                error: function (data) {
                }
            });
        }
    });

我在 DoSomething 方法中的模型 SubForm 总是 return null,但如果我将控制器方法参数更改为 :

它会起作用
 public ActionResult DoSomething(model Forum)

所以我的问题是:是否可以将不同的模型类型传递给控制器​​,或者 jquery ajax 仅当您在视图 (@model) 中传递完全相同的模型时才有效?

如有任何帮助,我们将不胜感激,对于英语不好,我们深表歉意。

可以使用Prefix属性的[Bind]属性

[HttpPost]
public ActionResult DoSomething([Bind(Prefix = "subForum")]SubForum model)

它目前不绑定的原因是发布的值是 subForum.test1="SomeValue",但由于 typeof SubForum 不包含名为 subForum 的 属性,绑定失败.该属性实质上去除了指定的前缀,因此它变成了 test1="SomeValue",它将绑定,因为 typeof SubForum 确实包含一个名为 test1

的 属性