.Net 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://localhost:4514/Customer/Details/2
当我单击 ActionLink 时,即使执行相同的代码,地址 url 不会更改为上面的。并且视图没有被呈现。
我做错了什么?
I have a Controller listed below:
//
// 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;
}
I have a View (where the ActionLink is) like below:
columns.Template(
@<text>
@Ajax.ActionLink("Detail", "Details", "Customer", new { id = item.CustomerID },
new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "detailCustomer" })
</text>
).Width(70);
The rendered output is now as follows:
<a data-ajax="true" data-ajax-mode="replace" data-ajax-update="#detailCustomer" href="/Customer/Details/2">Detail</a>
If I click on the link from within the source view of the browser, I get to my new View just fine.
However, if I try and click on the ActionLink, the View doesn't come up. I can verify during debugging, that I'm stepping thru the Details View after I hit that controller code. The present view just stays in place without switching to the new View.
Moreover, I can see that if I click on the ActionLink, it executes the same exact code (during debugging) as when I paste it into the address bar:
http://localhost:4514/Customer/Details/2
When I click on the ActionLink, even though the same code is executed, the address url doesn't change to the above. and the View is not being rendered.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用的是
Ajax.ActionLink
,这意味着它将发送 AJAX 请求。 AJAX 的重点是保持在同一页面上而不是重定向。您指定
UpdateTargetId = "detailCustomer"
,因此请确保您的页面中有一个具有此 id 的容器:它将使用 AJAX 调用的结果进行更新。还要确保您已将
jquery.unobtrusive-ajax.js
脚本正确包含到您的页面中,以便Ajax.ActionLink
帮助程序执行任何有用的操作。另一方面,如果您想执行完整的回发并更改浏览器的 url,您可能需要一个标准链接:
You are using an
Ajax.ActionLink
meaning that it will send an AJAX request. The whole point of AJAX is to stay on the same page and not redirect.You indicate
UpdateTargetId = "detailCustomer"
so make sure that in your page you have a container with this id:which will be updated with the results of the AJAX call. Also make sure that you have properly included the
jquery.unobtrusive-ajax.js
script to your page in order for theAjax.ActionLink
helper to do anything useful.On the other hand if you want to perform a full postback and change the url of your browser you probably need a standard link: