html.hidden未在asp.net MVC核心中设置值剃刀视图
我正在开发一个asp.net MVc核心应用程序。我有这样的表单元素的弹出:html.hidden未在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)
}
我有一个这样的视图模型:
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; }
}
我想在我看来,像这样填充它:
@Html.HiddenFor(m =>m.m_newIVR.Account, new { @value= Model.accountID})
但是看到视图源,值是NULL
我尝试使用:
@Html.Hidden("m.m_newIVR.Account", Model.accountID)
,它显示m_newIVR.Account填充。
然后我张贴的形式控制器这个动作
[HttpPost]
public ActionResult AddIVR(AccountDetailsViewModel model)
{
return RedirectToAction("AccountDetails", "mycontroller")
}
虽然我看到ACCOUNTID的视图(使用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>
我的问题是:
1)。为什么html.hiddenfor不是模型变量的设置值? 2)。虽然html.hidden是设置值,但为什么它不能在后处理方法中访问?
请建议。
感谢
现在我可以回答你的问题,为什么它的工作原理为Html.Hidden而不是Html.HiddenFor。
- 当你Html.HiddenFor与M => m.m_newIVR.Account那么它总是试图隐藏字段值在任何可利用m.m_newIVR.Account不是值,可以指定使用@value值设定值= Model.AccountId。
如果你想使用HiddenFor在ViewModel中设置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; }
}
控制器动作
[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" />
}
//样品从
我尝试了@ Html.HiddenFor(m => m.m_newIVR.Account)并在控制器操作中设置了m_newIVR.Account,但仍未设置其值。可能是什么原因? –
你能否提供那个返回该视图的Action? – dotnetstep
我已添加我的工作示例。确保它将绑定到Account not AccountId。 – dotnetstep
使用'新{@值= Model.accountID}'决不设置'value'属性。它不清楚你想要绑定什么,但是ir应该是'@ Html.HiddenFor(m => m.m_newIVR.Account)',并且在将模型传递给控制器方法之前设置'Account'的值风景。 –
而您的'Html.Hidden()'不起作用,因为它会生成'name =“m.m_newIVR.Account”'并且您的模型不包含名为'm'的属性 –
正在使用部分视图?什么@model在页面顶部?我怀疑是因为对于你说Model.AccountId的值,并且在隐藏中你使用了不同的模型。我试过它在我的模型中都适合我。 – dotnetstep