Search code examples
c#asp.net-core-mvc

ASP.NET Core MVC : passing one parameter (from ViewModel) from view to controller


I'm struggling with one case in my application I've main view when I display for admin user list of parameter.

Of course in my view I use data like a model (two string properties, name and value) and with display everything is ok, but when I click Edit (action link) and I want to pass just name of parameter to my edit method in controller (HttpGet) but I wanted pass string value not int but all the time I get null value in my method.

Can anyone tell me is if it is possible to pass a string parameter from my model?

My code looks something like this:

Viewmodel which I use

@model IEnumerable<ParametersDto>

My action link

@Html.ActionLink("Edit", "Edit", "Parameters", new { id=item.Name })</td>

Code:

[HttpGet]
public IActionResult Edit(string name)        
{
    // ...
}

In my method I tried string and my view model (object) like a type but always I have null value.


Solution

  • Change your action method's signature from

    public IActionResult Edit(string name)
    

    to

    public IActionResult Edit(string id) 
    

    That should allow the built in binder to hydrate the argument.