2011-10-17 39 views
1

我具有控制器列出如下:净MVC视图不渲染

// 
// GET: /Customer/Details/5 
public ActionResult Details(short id) 
{ 
    ActionResult actionResult = null; 

    if (HttpContext.User.IsInRole("Admin")) 
    { 
     // this is the logic that is getting executed 
     YeagerTechWcfService.Customer cust = db.GetCustomerID(Convert.ToInt16(id)); 
     actionResult = View("Details", cust);   } 
    else 
    { 
     HttpCookie cn = Request.Cookies["strCookieName"]; 
     if (cn != null) 
     { 
      YeagerTechWcfService.Customer cust = db.GetCustomerID(Convert.ToInt16(id)); 
      actionResult = View("Details", cust); 
     } 
     else 
     { 
      TempData["ErrCode"] = "CustView"; 
      actionResult = RedirectToAction("Index", "Home"); 
     } 
    } 

    return actionResult; 
} 

我有一个视图(其中ActionLink的是)象下面这样:

columns.Template(
    @<text> 
    @Ajax.ActionLink("Detail", "Details", "Customer", new { id = item.CustomerID }, 
    new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "detailCustomer" }) 
    </text> 
).Width(70); 

渲染输出现在是如下:

<a data-ajax="true" data-ajax-mode="replace" data-ajax-update="#detailCustomer" href="/Customer/Details/2">Detail</a> 

如果我点击从浏览器的源代码视图中,我到我的新视图就好了。

但是,如果我尝试点击ActionLink,则视图不会出现。我可以在调试过程中验证,当我点击该控制器代码后,我正在通过详细信息视图。目前的观点只是保持原位而不切换到新的视图。

而且,我可以看到,如果我点击ActionLink的,它执行完全相同的代码(在调试期间),当我将其粘贴到地址栏

的http://本地主机:4514 /客户//2

当我点击ActionLink的,即使相同的代码被执行详细信息,地址URL不会改变上述情况。和视图不呈现。

我在做什么错?

回答

5

即使执行相同的代码,地址URL没有您使用的是Ajax.ActionLink这意味着它将发送一个AJAX请求改变 上述

。 AJAX的重点在于保持在同一页面上,而不是重定向。

您表示UpdateTargetId = "detailCustomer"所以一定要确保在你的页面你有一个容器与此ID:

<div id="detailCustomer"></div> 

将与AJAX调用的结果进行更新。另外,请确保您已将jquery.unobtrusive-ajax.js脚本正确地包含在您的页面中,以便帮助Ajax.ActionLink帮助程序执行任何有用的操作。

在另一方面,如果你要执行一个完整的回传,改变你的浏览器的URL,你可能需要一个标准的链接:

Html.ActionLink("Detail", "Details", "Customer", new { id = item.CustomerID }, null) 
+0

达林,你是绝对正确的......我完全忘了我发送一个AJAX请求,这将使我保持在同一页面上,而不是重定向。我可以将其更改为您建议的HTML链接,或者在我的网格中执行AJAX回发以保持在同一页面上。对于细节,我会做一个HTML链接,对于编辑,我会保持它的AJAX链接,并让用户执行内联编辑。非常感谢。 – sagesky36