문제

Passing a param to the controller is null.. from examples ive seen im using correct overload. 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. To make this work you need to explicitly define the parameter in the URL as below:

<a href="/Customer/Index/?conName=trakman_Entities">trakman_Entities</a>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top