通过Ajax调用MVC传递时模型是否为Null? [英] Model is Null while passing by Ajax Call MVC?

查看:40
本文介绍了通过Ajax调用MVC传递时模型是否为Null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MVC中的新鸟.我想将模型传递给Ajax调用,我编写了以下代码.但是它总是将NULL传递给所有属性.

I am new bird in MVC. I want to pass the Model into the Ajax call, i write the following code to do so. But it always passing NULL to all properties.

   $("#btnsubmit").click(function () {

        alert('hello');
        var productModel = {
            Name: 'ram@mailinator.com',
            Address: "Chai"

        };
        $.ajax({
            type: "POST",
            url: '@Url.Action("ContactDistributor", "AjaxCallTest")',
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify({ model: productModel }),

            dataType: "json",
            success: function () { alert('Success'); },
            error: function (xhr) {
                alert(xhr.error);
            }
        });
        return false;

    });

型号:

    public class AjaxCalltestModal
{
    public int Id { get; set; }
    public String Name { get; set; }
    public String Address { get; set; }

}

控制器:

        [HttpPost]
    public ActionResult ContactDistributor(WebApplication1.Models.AjaxCalltestModal a)
    {
        return Json("test");
    }

请帮助我.

推荐答案

我已通过使用以下代码解决了该问题:

I have resolved the Issue by using the following Code:

$("#btnsubmit").click(function () {

    alert('hello');
    var productModel = {
        Name: 'ram@mailinator.com',
        Address: "Chai"

    };
    $.ajax({
        type: "POST",
        url: '@Url.Action("ContactDistributor", "AjaxCallTest")',
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({ 'model': productModel }),

        dataType: "json",
        success: function () { alert('Success'); },
        error: function (xhr) {
            alert(xhr.error);
        }
    });
    return false;

});

感谢您的努力和评论.

这篇关于通过Ajax调用MVC传递时模型是否为Null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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