我的观点中有一个表格:
@using (Html.BeginForm("ClearData", "MemberPass", FormMethod.Post))
{
<div>
@foreach (var property in ViewData.ModelMetadata.Properties)
{
@Html.Hidden(property.PropertyName, property.Model)
}
</div>
<button>Clear</button>
}
以及以下行动方法:
public ActionResult Index()
{
MemberPassInfoViewModel memberPassInfoViewModel = new ServiceUtilities().GetEventDetails(DateTime.Now.Date);
return View("Index", memberPassInfoViewModel);
}
public ActionResult GetMemberPassInfo(MemberPassInfoViewModel currentMemberPassInfoValues)
{
MemberPassInfoViewModel updatedMemberPassInfoViewModel = new ServiceUtilities().GetMemberPassInfoViewModel(currentMemberPassInfoValues);
return View("Index", updatedMemberPassInfoViewModel);
}
public ActionResult ClearData(MemberPassInfoViewModel currentMemberPassInfoValues)
{
MemberPassInfoViewModel updatedMemberPassInfoViewModel = new ServiceUtilities().ClearData(currentMemberPassInfoValues);
return View("Index", currentMemberPassInfoValues);
}
在调试中,我可以看到模型的两个属性存在。但是,当我查看生成的HTML时,其中一个属性具有空值:
<input id="SearchCode" name="SearchCode" type="hidden" value="23" />
<input id="FullName" name="FullName" type="hidden" value="" />
我注意到这可以通过替换:
来解决@Html.Hidden(property.PropertyName, property.Model)
使用:
<input type="hidden" value="@property.Model" name="@property.PropertyName" />
生成:
<input type="hidden" value="23" name="SearchCode" />
<input type="hidden" value="Alex Robert" name="FullName" />
为什么@Html.Hidden()
方法不起作用,但明确编写<input>
标记确实有效?