2012-07-23 86 views
0

我在想,如果这是一个全有或全无的情况。我想做什么,通过正常的MVC 3加载(GET)我的页面。控制器接受Model并将其传递给View。 View和Razor渲染它。但是,当我回发时,我希望它通过AJAX回发选定的信息。这可能吗?或者我有使用AJAX进行GET和POST吗?我需要关于GET和POST有关JQuery AJAX和MVC 3的一些说明

回答

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({ 
    url: myurl 
    // processData: false, // you may need this option depending on service setup 
    success: function(){ 
     location.href = "TARGET LOCATION"; 
    }, 
    type: "POST" 
}); 

进行目标定位:你将需要养活AJAX包含的变量任何网址如下产生

@URL.Action("Action", "Controller") 
0

http://knockoutmvc.com提供了一种将服​​务器端代码与客户端集成的好方法,它看起来可能会帮助您轻松实现您所需要的功能蚂蚁。

+2

请不要使用Knockout MVC。为每个功能发送呼叫到服务器是浪费。 – Tyrsius 2012-07-23 18:29:54

+1

我的那个动作重新knockoutmvc - 不要这样做 - 永远 - 请适当,相当请... – 2012-07-23 19:31:19