有效。为什么?-6ren"> 有效。为什么?-我现在真的很困惑,因为我认为@Html.HiddenFor(x => x.Id)和 是一样的,除了第一个变体的一些好处。但是,如果我使用第一个变体并查看生成的输入 html 值始终 = 1,而如果我使-6ren">
gpt4 book ai didi

c# - @Html.HiddenFor(x => x.Id) 不起作用,但 <input type ="hidden"value = @Model.Id> 有效。为什么?

转载 作者:行者123 更新时间:2023-11-30 16:10:32 27 4
gpt4 key购买 nike

我现在真的很困惑,因为我认为@Html.HiddenFor(x => x.Id)<input id="Id" name="Id" type="hidden" value=@Model.Id>是一样的,除了第一个变体的一些好处。但是,如果我使用第一个变体并查看生成的输入 html 值始终 = 1,而如果我使用第二个变体 - 一切正常。

这是我的 View 模型:

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 助手不能正常工作?

更新:

Controller 看起来像这样:

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

我的问题是因为我从一个方法返回另一个方法的结果吗?

最佳答案

尝试在操作方法中更改 id 名称,例如 questionId。

public ActionResult Questions(int  questionId)
{
return Create(new CheckList(), id);
}

更新:我找到了 SO 问题,这对你有帮助。

SO question

关于c# - @Html.HiddenFor(x => x.Id) 不起作用,但 &lt;input type ="hidden"value = @Model.Id> 有效。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25672235/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com