简体   繁体   English

ASP.NET MVC将ActionLink中的字符串传递给控制器

[英]ASP.NET MVC passing a string in an ActionLink to the controller

Passing a param to the controller is null.. from examples ive seen im using correct overload. 从参数传递给控制器​​的参数为null。 Any help much appriceated 任何有价值的帮助

@{
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());
    }

browser source code 浏览器源代码

 <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>

You should provide correct parameter name in action link and there is no need to specify parameters here :) 您应该在操作链接中提供正确的参数名称,并且无需在此处指定参数:)

@{
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>

}

If you are using MVC out of the box then it is only likely to work with a parameter called id that accepts an int. 如果您使用的是开箱即​​用的MVC,那么它只能与名为id的参数一起使用,该参数接受一个int。 To make this work you need to explicitly define the parameter in the URL as below: 为此,您需要在URL中明确定义参数,如下所示:

<a href="/Customer/Index/?conName=trakman_Entities">trakman_Entities</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM