【发布时间】:2011-05-12 15:24:08
【问题描述】:
我是一名新手网络程序员,所以如果我的一些“行话”不正确,请原谅我。 我有一个使用 MVC3 框架的 ASP.NET 项目。
我正在处理管理员视图,管理员将在其中修改设备列表。其中一个功能是“更新”按钮,我想在向 MVC 控制器发送帖子后使用 jquery 动态编辑网页上的条目。
我认为这种方法在单一管理设置中是“安全的”,在这种设置中,网页与数据库不同步的担忧微乎其微。
我创建了一个强类型视图,并希望使用 AJAX 帖子将模型数据传递给 MVC 控件。
在下面的帖子中,我发现了一些类似于我正在做的事情: JQuery Ajax and ASP.NET MVC3 causing null parameters
我将使用上面帖子中的代码示例。
型号:
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 控件的“最佳”方法?
我应该担心暴露模型信息吗?
【问题讨论】:
标签: asp.net-mvc-3 jquery