我正在开发一个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是设置值,但为什么它不能在后处理方法中访问?
请建议。
感谢
使用'新{@值= Model.accountID}'决不设置'value'属性。它不清楚你想要绑定什么,但是ir应该是'@ Html.HiddenFor(m => m.m_newIVR.Account)',并且在将模型传递给控制器方法之前设置'Account'的值风景。 –
而您的'Html.Hidden()'不起作用,因为它会生成'name =“m.m_newIVR.Account”'并且您的模型不包含名为'm'的属性 –
正在使用部分视图?什么@model在页面顶部?我怀疑是因为对于你说Model.AccountId的值,并且在隐藏中你使用了不同的模型。我试过它在我的模型中都适合我。 – dotnetstep