user1765862 user1765862 - 1 month ago 6
C# Question

sending hidden property under different name from razor to the controller

inside razor form I'm sending value using hidden property like

@Html.HiddenFor(model => model.Name)


this way I'm expecting on controller side parameter to be named as
name
in order to fetch this value.

public ActionResult(string MyName)
{

}


How can I send this value

@Html.HiddenFor(model => model.Name)


under different name, so I can on controller side receive that information like

public ActionResult(string MyName)
{

}

Answer

Use Html.Hidden(), not HiddenFor(). With Hidden you can pass in the name you want the element to be named.

Something like

@Html.Hidden("MyName", Model.Name)

See here

What is the difference between Html.Hidden and Html.HiddenFor

Comments