我想知道这是一种全有或全无的情况。我想做什么,通过普通MVC加载(GET)我的页面3.控制器接受模型并将其传递给View。视图和剃刀渲染它。但是,当我回发时,我希望通过AJAX回发所选信息。这可能吗?或者我是否使用AJAX进行GET和POST?
答案 0 :(得分:1)
使用其他方法GETting后,您当然可以使用AJAX进行POST。
这是一个关于SO的随机问题就是这样:
Ajax post in MVC 3 with multiple-form View
GET和POST操作根本不必相关。
答案 1 :(得分:1)
尝试以下内容。
控制器代码:
[HttpGet]
public ActionResult WhateverActionName()
{
YourViewModel yvm = new YourViewModel();
//Initalize viewmodel here
Return view(yvm);
}
[HttpPost]
public ActionResult WhateverActionName(YourViewModel yvm)
{
if (ModelState.IsValid) {
RedirectToAction("OtherAction", "OtherController")
}
return View(yvm);
}
的Ajax:
$.ajax({
url: myurl
// processData: false, // you may need this option depending on service setup
success: function(){
location.href = "TARGET LOCATION";
},
type: "POST"
});
对于目标位置:您将需要向ajax提供包含以下生成的任何URL的变量
@URL.Action("Action", "Controller")
答案 2 :(得分:0)
http://knockoutmvc.com提供了一种将服务器端代码与客户端集成的好方法,看起来它可以帮助您轻松实现您想要的目标。