Question

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>
Was it helpful?

Solution

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>

}

OTHER TIPS

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top