28

I could have used

@Html.HiddenFor(x=> ViewData["crn"])

but, I get,

<input id="ViewData_crn_" name="ViewData[crn]" type="hidden" value="500" />

To somehow circumvent that issue(id=ViewData_crn_ and name=ViewData[crn]), I tried doing the following, but the "value" attribute isn't getting set.

@Html.HiddenFor(x => x.CRN, new { @value="1"})
@Html.HiddenFor(x => x.CRN, new { @Value="1"})

generates

<input id="CRN" name="CRN" type="hidden" value="" />
<input Value="500" id="CRN" name="CRN" type="hidden" value="" />

Am I doing anything wrong?? Thanks

tereško
  • 58,060
  • 25
  • 98
  • 150
Sekhar
  • 5,614
  • 9
  • 38
  • 44
  • I don't think HiddenFor knows how to "read" values from ViewData. You can use Html.Hidden("fieldName", ViewData["crn"]) – Vasea Jul 27 '11 at 19:48
  • 1
    For anyone using MVC 4 please see @Gudradain answer below. – Yuck Feb 05 '14 at 02:00

6 Answers6

55

The following will work in MVC 4

@Html.HiddenFor(x => x.CRN, new { @Value = "1" });

@Value property is case sensitive. You need a capital 'V' on @Value.

Here is my model

public int CRN { get; set; }

Here is what is output in html when you look in the browser

<input value="1" data-val="true" data-val-number="The field CRN must be a number." data-val-required="The CRN field is required." id="CRN" name="CRN" type="hidden" value="1"/>

Here is my method

[HttpPost]
public ActionResult MyMethod(MyViewModel viewModel)
{
  int crn = viewModel.CRN;
}
Matt Cofer
  • 2,972
  • 1
  • 20
  • 18
Gudradain
  • 4,653
  • 2
  • 31
  • 40
  • 4
    This saved me so much frustration. `HiddenFor(t => t.Id)` doesn't work. Using `@value = Model.Id` didn't work. I had to use `@Value` - the capital `V` is apparently important. This is awful and must be a bug. – Yuck Feb 05 '14 at 02:00
  • 3
    Yes the capital V is important. Also, the @ is not required it seems. Both @Value and just Value works for me. – Gudradain Feb 06 '14 at 14:30
  • I am still new to MVC but where is you "MyMethod" The rest of the code I have is similar to yours but I don't know where your method goes. – mgrenier Apr 16 '15 at 14:42
  • @mgrenier It is simply a method in the controller. In this case, it is the method that will receive the form data after you post it from your browser. – Gudradain Apr 16 '15 at 15:30
  • Darin's answer is best, but this does work @Html.HiddenFor(x => x.CRN); I have used that when I had a method with a specific value that I binded to my model before it sent back to display the view thus for me comments.tblTipsId = id; return View(comments); Then on the page is @Html.HiddenFor(m => m.tblTipsId) – Tom Stickel Aug 14 '15 at 05:32
  • 1
    I have been using MVC for a few years now and I just realised that HiddenFor doesn't set value. That's a shame. Thanks for the answer. This really helped me. – Yatin Apr 18 '17 at 02:01
39

Have you tried using a view model instead of ViewData? Strongly typed helpers that end with For and take a lambda expression cannot work with weakly typed structures such as ViewData.

Personally I don't use ViewData/ViewBag. I define view models and have my controller actions pass those view models to my views.

For example in your case I would define a view model:

public class MyViewModel
{
    [HiddenInput(DisplayValue = false)]
    public string CRN { get; set; }
}

have my controller action populate this view model:

public ActionResult Index()
{
    var model = new MyViewModel
    {
        CRN = "foo bar"
    };
    return View(model);
}

and then have my strongly typed view simply use an EditorFor helper:

@model MyViewModel
@Html.EditorFor(x => x.CRN)

which would generate me:

<input id="CRN" name="CRN" type="hidden" value="foo bar" />

in the resulting HTML.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Sorry to comment on such an old answer, but wouldn't you want to use hiddenfor instead of editorfor if you want the input to be hidden. – Scott Adams Jun 04 '13 at 14:29
  • 2
    @ScottAdams, I have decorated the CRN property on my view model with the `[HiddenInput(DisplayValue = false)]` attribute which will make the EditorFor helper generate a hidden field. – Darin Dimitrov Jun 04 '13 at 15:10
  • `@Html.HiddenFor(model => model.AccionActual)` not working – Kiquenet Nov 22 '18 at 14:37
5

I believe there is a simpler solution. You must use Html.Hidden instead of Html.HiddenFor. Look:

@Html.Hidden("CRN", ViewData["crn"]);

This will create an INPUT tag of type="hidden", with id="CRN" and name="CRN", and the correct value inside the value attribute.

Hope it helps!

Marcio Paulo
  • 67
  • 1
  • 3
  • 4
    While this may appear simpler than [Darin's answer](http://stackoverflow.com/a/6850618/61654) it utilizes magic strings and therefore increases potential for problems during refactoring etc. – ahsteele Aug 16 '13 at 18:55
3

Keep in mind the second parameter to @Html.HiddenFor will only be used to set the value when it can't find route or model data matching the field. Darin is correct, use view model.

RickAndMSFT
  • 20,912
  • 8
  • 60
  • 78
0

A simple answer is to use @Html.TextboxFor but place it in a div that is hidden with style. Example: In View:

<div style="display:none"> @Html.TextboxFor(x=>x.CRN) </div>

Jane Cohen
  • 89
  • 1
  • 12
0

Simple way

@{
   Model.CRN = ViewBag.CRN;
}

@Html.HiddenFor(x => x.CRN)
Alireza
  • 455
  • 1
  • 5
  • 14