最佳答案
我是一个新手网络程序员,所以请原谅我,如果我的一些“行话”是不正确的。 我有一个使用 ASP.NET 使用 MVC3框架的项目。
我的工作在一个管理员视图,其中管理员将修改设备清单。其中一个函数是“ update”按钮,我想使用 jquery 在向 MVC 控制器发送文章后动态地编辑网页上的条目。
我认为这种方法是“安全的”在一个单一的管理设置,其中有最小的关注网页失去同步的数据库。
我已经创建了一个强类型的视图,希望通过 AJAX 帖子将模型数据传递给 MVC 控件。
在下面的帖子中,我发现了一些与我正在做的事情类似的东西: JQuery Ajax 和 ASP.NET MVC3导致空参数
我将使用上面文章中的代码示例。
型号:
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 控件的“最佳”方法?
我是否应该担心暴露模型信息?