我可以在MSDN上找到Html.HiddenFor的一个很好的定义,但我在Html.Hidden上找到的唯一的东西与它有的问题有关.
有人可以给我一个很好的定义和一个例子.
这个问题一直困扰着我两天.有一些类似的帖子,但没有一个完全解决我的问题.
使用MVC-3,Razor语法:
- EDIT.cshtml -
@using (Html.BeginForm("Edit", "My", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<!-- Some fields... -->
<div class="editor-field">
@Html.TextAreaFor(m => m.LongDescription)
@Html.ValidationMessageFor(m => m.LongDescription)
</div>
<!-- Some more fields work... Including picture upload (summary).-->
<input name="button" type="submit" value="Add Picture" />
<!-- Picture Item display -->
@foreach(var thumbnail in Model.ThumbnailImagePathAndNames)
{
<img src="@Url.Content(@thumbnail.ThumbnailPicturePath)" alt="" width="200" />
@Html.RadioButtonFor(o=>o.SelectedImage, @thumbnail.ImageGUID) Primary Picture
<!-- Checkbox to mark for deletion -->
@Html.CheckBoxFor(o=>thumbnail.Delete) Delete ???????? <!---- Here is a problem - I …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种智能方法来将列表的值存储在hidden input表单中,并在发布表单时将值返回到我的视图模型中。
在我的视图模型中,我有一个包含SelectListItem项目的列表。该列表的使用方式与元素asp-items中一样select。现在,当用户关闭脚本并发布有错误的表单时,我想重新显示表单而不必重新创建列表。
属性看起来像:
public IReadOnlyCollection<SelectListItem> UserLevels { get; set; }
Run Code Online (Sandbox Code Playgroud)
在剃刀视图中,这不会正确保存 UserLevels 的值:
<input type="hidden" asp-for="UserLevels"/>
Run Code Online (Sandbox Code Playgroud)
我发现了这个关于类似问题的问题,但我没有找到一个很好的解决方案。有人知道存储列表值的聪明方法吗?
我将List <>发送到View,然后将该列表转换为隐藏字段数组.这是使用局部视图完成的,以构建表.
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<BudgieMoneySite.Models.TransactionSplitLine>" %>
<tr>
<td>
<%=Model.Category %>
<%=Html.HiddenFor(x => x.CategoryId)%>
</td>
<td>
<%=Model.SubCategory %>
<%=Html.HiddenFor(x => x.SubCategoryId)%>
</td>
<td>
<%=Model.Amount %>
<%=Html.HiddenFor(x => x.AmountValue)%>
</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
我有x行....所以我希望我可以将隐藏字段中的值作为数组.我的模型中有一个字段,定义为public string [] CategoryIds {get; 组; }
(我认为这个问题可能是隐藏的领域被称为'CategoryId'和μ模型想要'CategoryIds').
渲染时,我看到我有一行看起来像这样:
<tr>
<td>
Medical
<input id="TransactionSplitLines_2__CategoryId" name="TransactionSplitLines[2].CategoryId" type="hidden" value="6" />
</td>
<td>
Over the Counter Medicines
<input id="TransactionSplitLines_2__SubCategoryId" name="TransactionSplitLines[2].SubCategoryId" type="hidden" value="22" />
</td>
<td>
111
<input id="TransactionSplitLines_2__AmountValue" name="TransactionSplitLines[2].AmountValue" type="hidden" value="0" />
</td>
Run Code Online (Sandbox Code Playgroud)
然后我试图将数据恢复到控制器中,如下所示:
[HttpPost]
public ActionResult AccountTransaction(AccountTransactionView model)
{
var reply = …Run Code Online (Sandbox Code Playgroud)