Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
761 views
Welcome To Ask or Share your Answers For Others

1 Answer

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...