Html.HiddenFor value property not getting set
Asked Answered
A

6

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

Antifriction answered 27/7, 2011 at 19:32 Comment(3)
I don't think HiddenFor knows how to "read" values from ViewData. You can use Html.Hidden("fieldName", ViewData["crn"])Longdrawn
For anyone using MVC 4 please see @Gudradain answer below.Sexology
Check this: codingfusion.com/Post/HTML-HiddenFor-set-value-in-Asp-Net-MVCIquique
C
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.

Cashbook answered 27/7, 2011 at 20:11 Comment(3)
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.Induline
@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.Cashbook
@Html.HiddenFor(model => model.AccionActual) not workingTinge
C
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;
}
Christman answered 27/11, 2013 at 21:10 Comment(6)
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.Sexology
Yes the capital V is important. Also, the @ is not required it seems. Both @Value and just Value works for me.Christman
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.Maren
@Maren 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.Christman
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)Peddada
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.Conductance
C
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.

Cashbook answered 27/7, 2011 at 20:11 Comment(3)
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.Induline
@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.Cashbook
@Html.HiddenFor(model => model.AccionActual) not workingTinge
M
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!

Malynda answered 6/2, 2012 at 0:15 Comment(1)
While this may appear simpler than Darin's answer it utilizes magic strings and therefore increases potential for problems during refactoring etc.Immorality
L
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.

Luannaluanne answered 28/7, 2011 at 19:17 Comment(0)
C
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>

Cordes answered 25/12, 2019 at 21:19 Comment(0)
L
0

Simple way

@{
   Model.CRN = ViewBag.CRN;
}

@Html.HiddenFor(x => x.CRN)
Leatheroid answered 13/7, 2020 at 5:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.