@ Html.HiddenFor (x => x.Id) Doesn't work, but <input type = "hidden" value = @ Model.Id> does. What for?
I am really very confused now because I thought that @Html.HiddenFor(x => x.Id)
and
<input id="Id" name="Id" type="hidden" value=@Model.Id>
are the same thing, except for some of the advantages of the first option. But if I use the first option and look at the generated html input value, always = 1, and if I use the second option, everything is fine.
So here is my model:
public class CheckListViewModel
{
public int Id { get; set; }
public DateTime CheckTime { get; set; }
public List<QuestionViewModel> Questions { get; set; }
}
My view code:
@using System.Diagnostics
@using WebCheckList.Models
@model CheckListViewModel
@using (Html.BeginForm("Submit", "CheckList", new {ReturnUrl = ViewBag.ReturnUrl}, FormMethod.Post, new {@class = "form-horizontal", role = "form"}))
{
//this one does not work. It generates the same html code as
// a line below it, only difference is that its value is always = 1
@Html.HiddenFor(x => x.Id);
//When I change it to this - everything works.
<input id="Id" name="Id" type="hidden" value=@Model.Id>
...
The MVC version is 5.2.0.0 Please tell me what I did wrong and why the @Html helper is not working as expected?
UPDATE:
The contoller looks like this:
public ActionResult Questions(int id)
{
return Create(new CheckList(), id);
}
public ActionResult Create(CheckList checkList, int checkListsTypeID = 2)
{
_db.CheckLists.Add(checkList);
_db.SaveChanges();
var checkListViewModel = new CheckListViewModel {Questions = questions, Id = checkList.ID, CheckTime = checkList.CheckTime};
return View("Details", checkListViewModel);
}
Is my problem because I am returning one method result from another?
+3
source to share