【发布时间】:2017-07-22 01:45:00
【问题描述】:
我尝试在 Asp.net MVC 5 中创建 ImageBrowser。问题是当我尝试切换到另一张图片时。 这是我的代码: 在视图中:
@model Katalog.Models.Model
@{
Model.enumerator = 0;
int count = Model.ImageList.Count;
int index = 1;
}
....
<table>
<tbody>
<tr>
<td> @index/@count </td>
....
</tr>
<tr>
@using (Html.BeginForm("previous", "Home",FormMethod.Post))
{
<td>
<input type="hidden" name="number" value="1" />
<input value="<" type="submit" onclick="ImageLeft()" class="buttonLeftRight"/>
</td>
}
<td>@{Html.RenderPartial("~/Views/Shared/ImageViews.cshtml", Model);}</td>
<td>
@using (Html.BeginForm("next", "Home", FormMethod.Post))
{
@Html.HiddenFor(a => a.ImageList)
@Html.HiddenFor(a => a.enumerator)
<input type="submit" class="buttonLeftRight" onclick="ImageRight()"/>
}
</td>
</tr>
</tbody>
</table>
....
<script>
function ImageRight()
{
@{ Model.enumerator++; }
}
</script>
我的控制器
....
public ActionResult next(Katalog.Models.Model model)
{
model = MyModel;
return View("Searcher",model);
}
....
和我的部分视图:
@model Katalog.Models.Model
<img id="foto" class="imgFinded" src="@Model.ImageList[@Model.enumerator]"/>
当我单击“下一步”按钮时,我的 model.ImageList 为空。为什么?
【问题讨论】:
-
您是否希望在单击按钮时增加
Model.enumerator的值(这永远不会发生) - 您所做的只是将enumerator的原始值传递给 POST 方法,因为这就是输入中的值 -
怎么做,你能给我举个例子吗?我是 asp.net 世界的新手,这是我的第一个应用程序。
-
有很多方法,但是使用您现有的代码只需使用
function ImageRight() { var e = Number($('#enumerator')++; $('#enumerator').val(e); }来更新隐藏输入中的值。 -
但是
ImageList是什么属性?顾名思义,它是一个集合,在这种情况下@Html.HiddenFor(a=>a.ImageList)将不起作用 - 查看您生成的 html!为什么还要考虑将所有这些数据发送到视图并再次感应到所有数据都原封不动 - 只需在 POST 方法中再次获取集合。 -
Property ImageList 是一个 List
使用 base64 解码的图像。我不知道如何在 Next() 方法中再次将 ImageList 填充为空
标签: c# asp.net-mvc image razor browser