2

将参数传递给控制器​​是空的。从我看到的示例中,我使用了正确的重载。任何帮助都非常感激

@{
foreach (string str in ViewBag.ServerNames)
{
    <ul> 
     <img src="../../Content/Images/my_computer.png" alt="Computer Name"/>
        <li >@Html.ActionLink(linkText: str.ToString(),actionName: "Index",controllerName:"Customer",
        routeValues:new{str = str.ToString()} , htmlAttributes: null)</li>
    </ul>

}

}

public ActionResult Index(string conName)
    {
        Response.Write("con name = " + conName);
        Response.End();
        string con = ConfigurationManager.ConnectionStrings[conName].ConnectionString;
        trakman_Entities db = new trakman_Entities(con);
        return View(db.customers.ToList());
    }

浏览器源代码

 <ul> 
     <img src="../../Content/Images/my_computer.png" alt="Computer Name"/>
        <li ><a href="/Customer/Index/DefaultConnection">DefaultConnection</a></li>
    </ul>
    <ul> 
     <img src="../../Content/Images/my_computer.png" alt="Computer Name"/>
        <li ><a href="/Customer/Index/trakman_Entities">trakman_Entities</a></li>
    </ul>
    <ul> 
     <img src="../../Content/Images/my_computer.png" alt="Computer Name"/>
        <li ><a href="/Customer/Index/trakman_Entities1">trakman_Entities1</a></li>
    </ul>
4

2 回答 2

6

您应该在操作链接中提供正确的参数名称,并且无需在此处指定参数 :)

@{
foreach (string str in ViewBag.ServerNames)
{
    <ul> 
     <img src="../../Content/Images/my_computer.png" alt="Computer Name"/>
        <li >@Html.ActionLink(str.ToString(),"Index","Customer",
        new{conName= str.ToString()} , null)</li>
    </ul>

}
于 2013-05-30T08:17:57.833 回答
0

如果您使用的是开箱即​​用的 MVC,那么它可能只与一个名为 id 的参数一起工作,该参数接受一个 int。要完成这项工作,您需要在 URL 中显式定义参数,如下所示:

<a href="/Customer/Index/?conName=trakman_Entities">trakman_Entities</a>
于 2013-05-30T08:20:17.380 回答