Search code examples
c#asp.net-mvcvisual-studioactionresulturl.action

Parameter not passing from @Url.Action to method


I'm trying to pass a field value from my model via @Url.Action to a method in my controller but it continues to come through as null. The link I'm sending the field value from looks like this

<input type="button" value="Submit" 
       onclick="location.href='@Url.Action("NewName", "Home", new { name = Model.name})'" />

The method in the controller looks like this:

public ActionResult NewName(string name)
{
     // .   
     // .
}

Each time the value for name passed to the method is null. What am I missing?


Solution

  • You can pass your value this way to the Controller:

    <input type="button" value="Submit" onclick="location.href='@Url.Action("NewName", "Home")?name=@Model.name';" />