Ajax Post:ASP.NET MVC操作正在接收空对象

Ajax Post:ASP.NET MVC操作正在接收空对象,ajax,asp.net-mvc,asp.net-mvc-partialview,Ajax,Asp.net Mvc,Asp.net Mvc Partialview,我有以下表格: @model CupCakeUI.Models.CupCakeEditViewModel @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "createFrm" })) { <div class="form-group"> <div class="editor-label"> @Html.LabelFor(model => m

我有以下表格:

@model CupCakeUI.Models.CupCakeEditViewModel
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "createFrm" }))
{
    <div class="form-group">
        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            <input  type="text" id="Name" name="Name" value="@Model.Name" />
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model)
        </div>
        <div class="editor-field">
            <input type="text" id="Price" name="Price" value="@Model.Price" />
        </div>
        <div class="col-md-offset-2 col-md-10">
            <input type="button" id="btnCreate" value="Create" class="btn btn-default" />
        </div>
    </div>
}
它在登录时在控制台中显示:

这是使用的操作方法和类:

[HttpPost]
public JsonResult Create (CupCakeUI.Models.CupCakeEditViewModel cupCakeEditModel)
{
    var cupCake =  
    CupCakeData.Save(cupCakeEditModel);
    return Json("cupCake", 
    JsonRequestBehavior.AllowGet);
 }
这门课:

public class CupCakeEditViewModel
{
    public int CupCakeId;
    [Display(Name = "CupCake Name")]
    public string Name;
    public string Price;
}
我也使用过这个,但不起作用:

$("#btnCreate").click(function () {
    var cupCakeEditModel = 
    $("#createFrm").serialize();
    $.ajax({
        url: "/CupCake/Create",
        type: "POST",
        data: cupCakeEditModel,
        success: function (response) {
            alert("Success");
        },
        error: function (response) {
        }
    });
})

我在论坛上找到了一些答案,但似乎有些奇怪

您的模型只包含字段,
DefaultModelBinder
不绑定字段,只绑定属性。将模型更改为

public class CupCakeEditViewModel
{
    public int CupCakeId { get; set; }
    [Display(Name = "CupCake Name")]
    public string Name { get; set; }
    public string Price { get; set; }
}

展示你的模型。(并且没有关系,但是你应该使用
@Html.TextBoxFor(m=>m.Name)
等)而不是手动创建Html)是的@StephenMuecke,我正在使用它,但我已经更改了所有内容以使其正常工作,这就是为什么我修改了所有内容。我用Action MethodWow在代码段中插入了类模型!我错过了。我从来没有看过它。顺便说一句,你的第二个选择(使用
.serialize()
更好,代码更少),你真的应该使用
url:'@url.Action(“Create”,“CupCake”)'
来确保url总是正确生成的。当然可以。无论如何,谢谢你。
public class CupCakeEditViewModel
{
    public int CupCakeId { get; set; }
    [Display(Name = "CupCake Name")]
    public string Name { get; set; }
    public string Price { get; set; }
}