【发布时间】:2017-11-15 15:20:53
【问题描述】:
首先,感谢您的回答。我对 Asp.Net MVC 上的路由有疑问。
您可以在我的控制器和视图所在的照片上看到我的解决方案。我想从“Index.cshtml”打开“UserProfile.cshtml”。好吧,我的第一种方法是使用 Ajax 在 ProfileController 中发布“UserProfile Action”。
$.ajax({
url: "/Profile/UserProfile",
type: "POST",
dataType: "json"})
.done(function(response){
window.location = '/Profile/UserProfile'});
此代码块进入控制器并点击“UserProfile”操作,但没有返回错误和视图。甚至 URL 也不会改变。
public ActionResult UserProfile()
{
return View ();
}
非常感谢您提供有用的回答和支持。谢谢!
【问题讨论】:
-
ASP.NET Core 无法处理 aspx 页面
-
您的视图需要是 Razor (.cshtml) 视图,并且默认路由在 App_Start 文件夹的 RouteConfig.cs 中定义
-
@peggy 谢谢。我已经更改为cshtml。它解决了 View 的 URL 问题。但下面的代码块仍然没有返回任何内容。它击中了那里,但没有错误,也没有再次查看。 public ActionResult UserProfile() { return View (); }
-
@Johnny 查看发布的答案。如果您仍有问题,请发布更新的屏幕截图,显示您已将视图更改为剃刀并发布视图代码。
-
你为什么要使用
window.location = '/Profile/UserProfile'- 使用ajax 的全部意义在于保持在同一页面上。如果要重定向,请不要使用 ajax。并且您指定dataType: 'json'但您的方法返回 html ,这样会引发错误。Remove thedataType`选项,改变方法返回一个PartialView并在回调中更新DOM——.done(function(response){ #(someElement).html(response); }更新当前页面
标签: c# asp.net asp.net-mvc routing