我现在非常困惑,因为我以为
所以这是我的视图模型:
我的问题是因为我从另一个方法返回了一个结果吗?
@Html.HiddenFor(x => x.Id)
和 <input id="Id" name="Id" type="hidden" value=@Model.Id>
是相同的,除了第一个变量具有一些优势。但是如果我使用第一个变量并查看生成的 HTML 值,则输入值始终为 1,如果我使用第二个变量,则一切正常。所以这是我的视图模型:
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" value=@Model.Id>
...
MVC版本为5.2.0.0。请告诉我,我做错了什么,为什么@Html助手无法正常工作?
更新:
控制器如下:
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);
}
我的问题是因为我从另一个方法返回了一个结果吗?