I have a HiddenFor field that is missing it's value tag. This is on a MVC4 project using the razor view engine.
<input name="Id" id="teamId" type="hidden" data-val-required="The Id field is required." data-val-number="The field Id must be a number." data-val="true"/>
This is generated from:
@Html.HiddenFor(m => m.Id, new { id = "teamId" })
From my model (using System.ComponentModel.DataAnnotations):
public class TeamModel
{
public int Id { get; set; }
[Required(ErrorMessage = "Please enter a team name"), MaxLength(25)]
public string Name { get; set; }
}
When passing a new model to the form I've even tried manually setting the Id field like so:
return PartialView("EditTeam", new TeamModel{Id = 5});
This still doesn't display a value however. Oddly it's passing client validation but failing server side validation. I have a similar setup in another project which works fine. I can't see any differences between them. Is there something obvious that I've missed? Thanks :)
edit: If I use:
@Html.Hidden("test", Model.Id)
It works. Something with the HiddenFor is setting the value to blank (seeing the raw HTML the value field is there but just set to: value="")
edit: For completeness sake, here is the controller (also the root of the problem):
public ActionResult GetTeam(int? id)
{
return PartialView("EditTeam", id.HasValue ? _teamLogic.GetById(id.Value) : new TeamModel());
}