MVC3 HTML ActionLink无法正常工作

时间:2012-07-10 20:17:08

标签: asp.net-mvc-3

我有一个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");
        }

    }

我有另一个masterlayout文件,我有这个代码

<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而不是工作:(

我现在该怎么办?

4 个答案:

答案 0 :(得分:1)

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

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

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

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

答案 1 :(得分:1)

  • LinkText:“信息中心”
  • ActionName:“index”
  • ControllerName:“仪表板”

    @ Html.ActionLink(“仪表板”,“索引”,“仪表板”)

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

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

答案 2 :(得分:0)

使用this重载

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

这是格式

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

答案 3 :(得分:0)

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

尝试替换

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

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

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

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