正确的方法使用AJAX张贴在jQuery来从强类型MVC3视图模型传递 [英] Proper way to use AJAX Post in jquery to pass model from strongly typed MVC3 view

查看:117
本文介绍了正确的方法使用AJAX张贴在jQuery来从强类型MVC3视图模型传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新手Web程序员所以请原谅我,如果我的一些行话是不正确的。
我一直在使用ASP.NET使用MVC3框架有一个项目。

I'm a novice web programmer so please forgive me if some of my "jargon" is not correct. I've got a project using ASP.NET using the MVC3 framework.

我工作的一个管理视图,管理员会修改设备清单。其中一个功能是更新按钮,我想使用jQuery发送邮寄到MVC控制器后动态编辑网页上的条目。

I am working on an admin view where the admin will modify a list of equipment. One of the functions is an "update" button that I want to use jquery to dynamically edit the entry on the webpage after sending a post to the MVC controller.

我presume这种做法是在一个单一的管理环境,在那里有网页不同步与数据库的最小关注安全的。

I presume this approach is "safe" in a single admin setting where there is minimal concern of the webpage getting out of sync with the database.

我已经创建了一个强类型的视图,并希望到模型数据传递到使用AJAX后MVC的控制。

I've created a view that is strongly typed and was hoping to pass the model data to the MVC control using an AJAX post.

在下面的文章中,我发现了一些类似于我所期待的做:
<一href=\"http://stackoverflow.com/questions/5698888/jquery-ajax-and-asp-net-mvc3-causing-null-parameters\">JQuery Ajax和ASP.NET MVC3导致空参数

In the following post, I found something that is similar to what I am looking at doing: JQuery Ajax and ASP.NET MVC3 causing null parameters

我会用code样品从上面的帖子。

I will use the code sample from the above post.

型号:

public class AddressInfo 
{
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }
}

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Check(AddressInfo addressInfo)
    {
        return Json(new { success = true });
    }
}

脚本查看:

<script type="text/javascript">
var ai = {
    Address1: "423 Judy Road",
    Address2: "1001",
    City: "New York",
    State: "NY",
    ZipCode: "10301",
    Country: "USA"
};

$.ajax({
    url: '/home/check',
    type: 'POST',
    data: JSON.stringify(ai),
    contentType: 'application/json; charset=utf-8',
    success: function (data.success) {
        alert(data);
    },
    error: function () {
        alert("error");
    }
});
</script>

我还没有机会使用上述呢。但我在想,如果这是最好的方法传递模型数据回使用AJAX的MVC控制?

I have not had a chance to use the above yet. But I was wondering if this was the "best" method to pass the model data back to the MVC control using AJAX?

我应该关心露出模型的信息?

Should I be concerned about exposing the model information?

推荐答案

您可以跳过VAR声明和字符串化。否则,将工作得很好。

You can skip the var declaration and the stringify. Otherwise, that will work just fine.

$.ajax({
    url: '/home/check',
    type: 'POST',
    data: {
        Address1: "423 Judy Road",
        Address2: "1001",
        City: "New York",
        State: "NY",
        ZipCode: "10301",
        Country: "USA"
    },
    contentType: 'application/json; charset=utf-8',
    success: function (data) {
        alert(data.success);
    },
    error: function () {
        alert("error");
    }
});

这篇关于正确的方法使用AJAX张贴在jQuery来从强类型MVC3视图模型传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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