Note that there are some explanatory texts on larger screens.

plurals
  1. POHiddenFor missing value tag
    text
    copied!<p>I have a HiddenFor field that is missing it's value tag. This is on a MVC4 project using the razor view engine.</p> <pre><code>&lt;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"/&gt; </code></pre> <p>This is generated from:</p> <pre><code>@Html.HiddenFor(m =&gt; m.Id, new { id = "teamId" }) </code></pre> <p>From my model (using System.ComponentModel.DataAnnotations):</p> <pre><code>public class TeamModel { public int Id { get; set; } [Required(ErrorMessage = "Please enter a team name"), MaxLength(25)] public string Name { get; set; } } </code></pre> <p>When passing a new model to the form I've even tried manually setting the Id field like so:</p> <pre><code>return PartialView("EditTeam", new TeamModel{Id = 5}); </code></pre> <p>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 :)</p> <p>edit: If I use:</p> <pre><code>@Html.Hidden("test", Model.Id) </code></pre> <p>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="")</p> <p>edit: For completeness sake, here is the controller (also the root of the problem):</p> <pre><code>public ActionResult GetTeam(int? id) { return PartialView("EditTeam", id.HasValue ? _teamLogic.GetById(id.Value) : new TeamModel()); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload