Kelly Cline
Kelly Cline

Reputation: 2246

HiddenInput doesn't travel

I have a form with these statements:

@Html.HiddenFor(model => model.CompanyId)
@Html.HiddenFor(model => model.OriginalDailyTimeOption)
@Html.HiddenFor(model => model.OriginalWorkWeekStartDay)

When I View Source, I see these elements in the form element, and when I submit the form, these properties are all properly returned.

Now, I read about using [HiddenInput] in my viewmodel, so I tried this:

[HiddenInput(DisplayValue = true)]
public int CompanyId { get; set; }

// Original values
[HiddenInput(DisplayValue = true)]
public DayOfWeek OriginalWorkWeekStartDay { get; set; }

[HiddenInput(DisplayValue = true)]
public bool OriginalDailyTimeOption { get; set; }

and I removed the @Html statements that I had before.

Now, when I View Source, I do not see the elements in the form, and when I submit the form, the values do not show up at the controller action. I had also tried the HiddenInput attribute without having (DisplayValue = true), but I got the same results. I also tried DisplayValue = false.

Is there something I am doing wrong with my HiddenInput attributes? (For the record, I am testing with IE 11.)

Upvotes: 1

Views: 782

Answers (2)

Kelly Cline
Kelly Cline

Reputation: 2246

I found this in another post (by podiluska) when I searched for "difference between Html.HiddenFor & HiddenInput attribute":

"HiddenInput is an attribute on a field in the (view)model - which means that it can be used across multiple views and indicates that this field should be hidden wherever it is rendered by an EditorFor helper."

The MSDN page on HiddenInputAttribute says nothing about EditorFor, so that's what I was missing.

Upvotes: 1

GlobalJim
GlobalJim

Reputation: 1189

Include a [HttpPost] Also make sure the hiddenfor's are in your form

[HttpPost]
public ActionResult action(Model model)
{
     Model _model = new Model();
     _model.CompanyId = model.CompanyId;
    etc...


}

Upvotes: 0

Related Questions