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

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

问题描述

我是一个新手网络程序员,所以如果我的一些行话"不正确,请原谅我.我有一个使用 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.

我正在处理管理员视图,管理员将在其中修改设备列表.其中一个功能是更新"按钮,我想在向 MVC 控制器发送帖子后使用 jquery 动态编辑网页上的条目.

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.

我认为这种方法在单个管理员设置中是安全的",在这种情况下网页与数据库不同步的担忧最小.

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.

在下面的帖子中,我发现了一些与我正在做的事情类似的事情: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

我将使用上述帖子中的代码示例.

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 声明和 stringify.否则,这将工作得很好.

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");
    }
});

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

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