我正在开发一个 asp.net MVc 核心应用程序。我有一个带有这样的表单元素的弹出窗口:
@using (Html.BeginForm("AddIVR", "ITPVoice", FormMethod.Post, new { role = "form" }))
{
@*@Html.HiddenFor(m =>m.m_newIVR.Account, new { @value= Model.accountID})*@
@Html.Hidden("m.m_newIVR.Account", Model.accountID)
}
我有一个像这样的 View 模型:
public class AccountDetailsViewModel
{
public IVRS m_newIVR { get; set; }
}
和像这样的 IVRS 模型类:
public class IVRS
{
[JsonProperty("_id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("account")]
public string Account { get; set; }
}
我正在尝试像这样在我的 View 中填充它:
@Html.HiddenFor(m =>m.m_newIVR.Account, new { @value= Model.accountID})
但是当我看到查看源代码时,值为空
我尝试使用:
@Html.Hidden("m.m_newIVR.Account", Model.accountID)
它显示 m_newIVR.Account 已填充。
然后我发布表单来控制这个 Action
[HttpPost]
public ActionResult AddIVR(AccountDetailsViewModel model)
{
return RedirectToAction("AccountDetails", "mycontroller")
}
虽然我看到 AccountId 已填充在 View 中(使用 viewsource),但在 model.m_newIVR.Account 的后操作方法中值为空。
HTML 输出如下所示:
<div id="edit-ivrs-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
<div class="modal-dialog">
<form action="/ITPVoice/AddIVR" method="post" role="form"> <div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Add IVR</h4>
<input id="m_newIVR_Account" name="m_newIVR.Account" type="hidden" value="" />
<input id="AccountId" name="AccountId" type="hidden" value="56f5e3d77ea022a042665be1" />
</div>
<div class="modal-body">
</div>
</div>
我的问题是:
- 为什么 html.hiddenfor 没有设置模型变量的值?
- 虽然html.hidden是设置值,但为什么在post action方法中访问不到?
请提出建议。
最佳答案
现在我可以回答您的问题,为什么它适用于 Html.Hidden 但不适用于 Html.HiddenFor。
- 当您使用 Html.HiddenFor 和 m =>m.m_newIVR.Account 时,它总是尝试为隐藏字段值设置值,无论属性 m.m_newIVR.Account 中可用的值是什么值,而不是您使用 @value = Model 指定的值。帐户ID。
如果你想在 ViewModel 中使用 HiddenFor 设置 m_newIVR.Account 只需使用以下内容。
@Html.HiddenFor(m =>m.m_newIVR.Account)
- Html.Hidden 不是强类型,因此它不依赖于名称。您可以指定不同的名称和值参数。在这种情况下,您有责任为 HiddenField 生成适当的名称。
我的工作示例
型号
public class AccountDetailsViewModel
{
public string AccountId { get; set; }
public IVRS m_newIVR { get; set; }
}
public class IVRS
{
[JsonProperty("_id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("account")]
public string Account { get; set; }
}
Controller 操作
[HttpGet]
public IActionResult Index1()
{
AccountDetailsViewModel model = new AccountDetailsViewModel();
//model.AccountId = "1222222";
model.m_newIVR = new IVRS();
model.m_newIVR.Account = "122222";
return View(model);
}
[HttpPost]
public IActionResult Index1(AccountDetailsViewModel model)
{
return View(model);
}
查看 (Index1.cshtml)
@model WebApplication2.Controllers.AccountDetailsViewModel
@using (Html.BeginForm())
{
@Html.HiddenFor(m =>m.m_newIVR.Account)
<input type="submit" />
}
//抽样
关于c# - html.hidden 未在 asp.net MVC 核心 Razor View 中设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39501843/