我是一个新手网络程序员,所以请原谅我的一些“行话”是不正确的。我有一个使用ASP.NET的项目,该项目使用MVC3框架。
我在一个管理员视图中工作,管理员将修改设备列表。其中一个功能是“更新”按钮,我希望在向MVC控制器发送post后使用jquery动态编辑网页上的条目。
我假设这种方法在单一的管理设置中是“安全的”,在这种设置中,网页与数据库不同步的问题最少。
我已经创建了一个强类型的视图,并希望使用AJAX post将模型数据传递给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控件的“最佳”方法?
我应该担心暴露模型信息吗?
发布于 2011-05-13 01:11:43
您可以跳过var声明和stringify。否则,这将工作得很好。
$.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");
}
});
发布于 2011-08-13 04:38:23
这就是它为我工作的方式:
$.post("/Controller/Action", $("#form").serialize(), function(json) {
// handle response
}, "json");
[HttpPost]
public ActionResult TV(MyModel id)
{
return Json(new { success = true });
}
https://stackoverflow.com/questions/5980389
复制相似问题