我真的很困惑现在,因为我认为@Html.HiddenFor(x => x.Id)
和 <input id="Id" name="Id" type="hidden" [email protected]>
是同样的事情,除了第一个变种的一些好处。但是,如果我使用第一个变体并查看生成的html值,输入总是= 1,并且如果使用第二个变体 - 一切正常。@ Html.HiddenFor(x => x.Id)不起作用,但<input type =“hidden”value = @ Model.Id>起作用。为什么?
因此,这里是我的视图模型:
public class CheckListViewModel
{
public int Id { get; set; }
public DateTime CheckTime { get; set; }
public List<QuestionViewModel> Questions { get; set; }
}
我的视图代码:
@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" [email protected]>
...
MVC的版本是5.2.0.0 请告诉我,我做了什么错,为什么@Html帮手不能正常工作?
UPDATE:
位指示如下:
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);
}
是我的问题发生,因为我从另一个返回一个方法的结果?
我想你应该提供控制器code.Dou你在控制器代码chabge ID? – cosset 2014-09-04 18:12:58
@cosset,更新 – Anarion 2014-09-04 18:18:36