0

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());
}
Tikeb
  • 978
  • 3
  • 9
  • 25

3 Answers3

0

have you tried:

@Html.HiddenFor(m => m.Id, new { id = "teamId", value=Model.Id })
Vic
  • 632
  • 6
  • 15
  • It seems like that should have worked but it didn't. Value still comes through as empty/blank. – Tikeb Jul 09 '13 at 12:05
  • It's strange, i have just tested in my own code and it is working :S – Vic Jul 11 '13 at 16:43
  • Yea sorry I missed the controller in the initial question. I'll update the question and flag my answer as done – Tikeb Jul 17 '13 at 07:48
0

Why do you need the second parameter?

@Html.HiddenFor(m => m.Id, new { id = "teamId" })

If you return your model correctly from the controller, this will be enough:

@Html.HiddenFor(m => m.Id)
speti43
  • 2,886
  • 1
  • 20
  • 23
  • I added it just a way to identify it easier when debugging. It's the same with or without. – Tikeb Jul 09 '13 at 12:04
  • Maybe this conversation with similar problem, helps you http://stackoverflow.com/questions/6850174/html-hiddenfor-value-property-not-getting-set – speti43 Jul 09 '13 at 12:37
  • For me `@Html.HiddenFor(m => m.Id)` doesn't work. ***How-to return your model correctly*** ? – Kiquenet Nov 22 '18 at 14:41
0

I'll answer my own question here as I've managed to work out the problem. I should have added this code to the question (this is the controller that is called to load the modal with the form):

public ActionResult GetTeam(int? id)
{
    return PartialView("EditTeam", id.HasValue ? _teamLogic.GetById(id.Value) : new TeamModel());
}

The problem was that the argument being passed in was 'id'. When changed to 'teamId' it worked. I'm guessing MVC was trying to be clever and identified it as a key value on the modal so when calling new, sets it as blank.

Tikeb
  • 978
  • 3
  • 9
  • 25