Ajax Post:ASP.NET MVC操作正在接收空对象 [英] Ajax Post: ASP.NET MVC action is receiving empty object

查看:212
本文介绍了Ajax Post:ASP.NET MVC操作正在接收空对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个表格:

@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>
}

我正在尝试使用ajax post将数据发送到Action方法,但是它始终接收空对象.我过去已经做过几次,现在我尝试了不同的方法,这些方法不起作用,代码:

I am trying to use ajax post to send data to the Action Method, however its always receiving empty object. I have done that several times in the past, and now i tried different ways which not working, The code:

$(document).ready(function () {
    $("#btnCreate").click(function () {
        var name = $("#Name").val();
        var price = $("#Price").val();
        var cupCakeEditModel = { "CupCakeId": 0, "Name": name, "Price": price };
        var json = JSON.stringify(cupCakeEditModel);
        $.ajax({
            type: 'POST',
            url: "/CupCake/Create",
            data: JSON.stringify(cupCakeEditModel),
            contentType: 'application/json',
            success: function () {
                alert("succes");
            },
            error: function () {
                alert("error");
            }
        });
    })
})

它在登录时在控制台中显示:

Its showing this in the console when logging:

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

This is the Action Method and Class used:

[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;
}

我也用过,但是不能用:

I have also used this, but not working:

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

还有我在论坛上找到的几个答案,但这似乎有些不可思议!

And several answers i found on the forum, but it seems something weird!

推荐答案

您的模型仅包含字段,而DefaultModelBinder不绑定字段,仅绑定属性.将模型更改为

You model contains only fields, and the DefaultModelBinder does not bind fields, only properties. Change the model to

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

这篇关于Ajax Post:ASP.NET MVC操作正在接收空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆