【问题标题】:MVC3 HTML ActionLink not workingMVC3 HTML ActionLink 不起作用
【发布时间】:2013-01-20 14:26:02
【问题描述】:

我有一个 DashBoardController.cs 我有这个代码

public class DashBoardController : Controller
    {
        //
        // GET: /DashBoard/

        [Authorize]
        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /New Project/

        [Authorize]
        public ActionResult NewProject()
        {
            return View();
        }

        //
        // GET: /File Upload/

        public ActionResult UploadFile()
        {
            return View();
        }

        [HttpPost]
        public ActionResult UploadFile(HttpPostedFileBase file)
        {
            // Verify that the user selected a file
            if (file != null && file.ContentLength > 0)
            {
                // extract only the fielname
                var fileName = Path.GetFileName(file.FileName);
                // store the file inside ~/App_Data/uploads folder
                var path = Path.Combine(Server.MapPath("~/Uploads"), fileName);
                file.SaveAs(path);
            }
            // redirect back to the index action to show the form once again
            return RedirectToAction("Index", "Home");
        }

    }

我这里有另一个主布局文件我有这个代码

<div id="LeftColumn" class="ui_style_floatLeft">
            <div id="menuWrapper">
                <ul class="menu">
                    <li class="menuDashBoard">@Html.ActionLink("Dashboard","Index")</li>
                    <li class="menuProject"><a href="#">Project</a>
                        <ul>
                            <li>@Html.ActionLink("New Project","NewProject")</li>
                            <li><a href="#">Projects</a></li>
                        </ul>
                    </li>                   
                    <li class="menuAccount"><a href="#">Account</a>
                        <ul>
                            <li>@Html.ActionLink("Change Password", "ChangePassword", "Account")</li>
                        </ul>
                    </li>                  
                </ul>               
             </div>
        </div>

但如果我转到 Change Password 操作链接,那么其他链接(New ProjectDashboard)将无法正常工作。我尝试 @Url.Action 进入 herf attr 但不工作:(

我现在该怎么办?

【问题讨论】:

    标签: asp.net-mvc-3


    【解决方案1】:
    • LinkText:“仪表板”
    • 动作名称:“索引”
    • 控制器名称:“仪表板”

      @Html.ActionLink("Dashboard", "index", "dashboard")

    如果您使用areas 将控制器分组到您需要的不同区域。

    @Html.ActionLink("Dashboard", "index", "dashboard", new { area = "YourAreaName"})
    

    【讨论】:

    • 我是 MVC3 的新手。 Area 究竟做了什么?你能给我一个链接,我可以知道Area吗?
    【解决方案2】:

    您需要在action link 中包含控制器名称:

    @Html.ActionLink("Dashboard","Index","DashBoard")
    

    如果您省略了controllerName,则链接将使用当前控制器 构建。由于您导航到 AccountController,因此应该指向 DashboardController 的链接坏了。

    在共享区域(如导航)中,您通常需要包含控制器引用。

    【讨论】:

    • 是的,但它只适用于此@Html.ActionLink("New Project","NewProject","Project")@Html.ActionLink("Dashboard","Index","DashBoard") 不起作用:(
    【解决方案3】:

    使用this重载

    @Html.ActionLink("New Project","NewProject","DashBoard")
    

    这是格式

    public static MvcHtmlString ActionLink(
        this HtmlHelper htmlHelper,
        string linkText,
        string actionName,
        string controllerName
    )
    

    【讨论】:

    • 谢谢我的朋友,但它仅适用于 NewProject 链接,但不适用于 DashBoard Index :(
    • 您是否对索引操作的链接使用了相同的重载?
    【解决方案4】:

    仔细看一下,我认为您没有为"NewProject""Index" 方法放置控制器

    尝试替换

    @Html.ActionLink("New Project","NewProject")
    

    @Html.ActionLink("New Project","NewProject", "DashBoard")
    

    @Html.ActionLink("Dashboard","Index")
    

    @Html.ActionLink("Dashboard","Index", "DashBoard")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-19
      • 2012-04-08
      • 1970-01-01
      • 2013-07-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多