我目前正在研究 Visual Studio MVC。由于 MVC 有助于为某些功能自动创建代码。我试图理解这句代码是什么意思。我理解前两个参数,但不理解第三个参数。
-----> @Html.ActionLink("Text Displayed", "Method in controller", "Third???")
我的 Controller 代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MP.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
下面是我看不懂的代码:
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
最佳答案
第三个参数是 Controller 的名称。在您的情况下,您有一个名为 HomeController
的 Controller ,因此第三个参数是 "Home"
(按照惯例,您省略了“Controller”后缀)。
如果使用@Html.ActionLink()
方法的页面是由HomeController
中的方法生成的,可以省略第三个参数,因为例如,在您的 About
页面中,您可以使用
<li>@Html.ActionLink("Home", "Index")</li>
然而,如果 Index()
方法在(比如说)ProductController
中,那么它就需要是
<li>@Html.ActionLink("Home", "Index", "Product")</li>
关于c# - ASP.Net 对导航栏按钮使用 @Html.ActionLink(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30858713/