1

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)
{

}
user1765862
  • 13,635
  • 28
  • 115
  • 220
  • I don't see the difference on the samples? Maybe a typo? – Karel Tamayo Oct 27 '16 at 13:27
  • @KarelTamayo I think they are asking how they can send their Hidden, which uses model.Name, to post to the controller's MyName value – Alfie Goodacre Oct 27 '16 at 13:29
  • There is really no point using `HtmlHelper`method is this case (the whole point of using the helpers is for correct 2-way model binding, implementing validation etc and your defeating all that) You may as well just manually create the input ` –  Oct 27 '16 at 21:44

2 Answers2

3

If you use set Name attribute same as your parameter then in works fine.

   @Html.HiddenFor(model => model.Name, new { id = "MyName", name = "MyName"})

View Look like View

Controller looks

enter image description here

Lalji Dhameliya
  • 1,729
  • 1
  • 17
  • 26
2

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

Community
  • 1
  • 1
Fran
  • 6,440
  • 1
  • 23
  • 35