【问题标题】:Image Browser Asp.net MVC 5图片浏览器 Asp.net MVC 5
【发布时间】: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=&gt;a.ImageList) 将不起作用 - 查看您生成的 html!为什么还要考虑将所有这些数据发送到视图并再次感应到所有数据都原封不动 - 只需在 POST 方法中再次获取集合。
  • Property ImageList 是一个 List 使用 base64 解码的图像。我不知道如何在 Next() 方法中再次将 ImageList 填充为空

标签: c# asp.net-mvc image razor browser


【解决方案1】:

两个 Javascript 函数中的代码 @{Model.enumerator++;} 和 @{Model.enumerator--;} 是服务器端代码,因此它只会在视图在服务器上呈现时和之前执行一次传递给客户端浏览器。所以通过按下提交按钮触发 onclick="ImageRight()" 不会改变服务器端的值。

您可以尝试将当前索引发布到控制器中的操作,并根据调用的操作增加或减少它。

@using (Html.BeginForm("Next", "Home", FormMethod.Post))
{ 
    @Html.HiddenFor(a=>a.CurrentIndex)
    <input type="submit"  class="buttonRightLeft"/>
}

@using (Html.BeginForm("Previous", "Home", FormMethod.Post))
{ 
    @Html.HiddenFor(a=>a.CurrentIndex)
    <input type="submit"  class="buttonLeftRight"/>
}


public ActionResult Next(int CurrentIndex)
{
    // Get the NEXT image and return as model
    model = MyModel;
    return View("Searcher",model);
}

public ActionResult Previous(int CurrentIndex)
{
    // Get the PREVIOUS image and return as model
    model = MyModel;
    return View("Searcher",model);
}

【讨论】:

    【解决方案2】:

    您的模型ImageList 属性是null 的原因是因为您生成了一个隐藏输入@Html.HiddenFor(a =&gt; a.ImageList),它会生成

    <input name="ImageList" value="System.Collections.Generic.List[]String" .. />
    

    它不会在 POST 方法中绑定到您的集合(当某些东西未绑定时,请始终使用表单的 namevalue 属性控制您的生成。

    为了绑定到该列表,您需要使用循环为列表中的每个项目生成一个输入。

    另一个问题是您的脚本什么都不做。您的模型是服务器端代码,您不能使用 javascript 增加模型属性的值 - 您需要向控制器发送请求以执行此操作。

    由于您将图像集合发送到视图,因此无需向服务器发回请求 - 您只需更新 &lt;img&gt; 标记的 src 属性即可。

    把模型改成

    public class Model
    {
        public int InitialIndex { get; set; }
        public int ImageCount { get { return ImageList.Count; } }
        public List<string> ImageList { get; set; }
    }
    

    然后视图可以只是(部分是不必要的)

    <img id="foto" class="imgFinded" src="@Model.ImageList[@Model.InitialIndex ]"/>
    <button type="button" id="previous">Previous</button>
    <button type="button" id="next">Next</button>
    

    和脚本

    var imagelist = @Html.Raw(Json.Encode(Model.ImageList));
    var index = @Html.Raw(Json.Encode(Model.InitialIndex));
    var max = @Html.Raw(Json.Encode(Model.ImageCount));
    var image = $('#foto');
    
    // Set the initial state of the buttons
    if (index === 0) {
        $('#previous').prop('disabled', true);
    } else if (index === max) {
        $('#previous').prop('disabled', true);
    }
    
    $('#next').click(function() {
        $('#previous').prop('disabled', false);
        index++;
        image.attr('src', imagelist[index]);
        if (index === max) {
            $(this).prop('disabled', true);
        }
    })
    $('#previous').click(function() {
        $('#next').prop('disabled', false);
        index--;
        image.attr('src', imagelist[index]);
        if (index === 0) {
            $(this).prop('disabled', true);
        }
    })
    

    【讨论】:

    • Json.Encode 有问题。我将组件引用 System.Web.Helpers 添加到我的项目中,并在顶部写了@using System.Web.Helpers;。而且我的 Visual Studio 仍然无法识别 Json 类。怎么办?
    • 你不需要做任何这些。 System.Web.Helpers.dll 默认已包含在内,您的 web.config 文件应该已经包含 &lt;pages&gt;&lt;namespaces&gt;&lt;add namespace="System.Web.Helpers" /&gt; .... 我最好的猜测是您在 web.config 文件中的版本控制方面存在一些问题。尝试创建一个新项目,看看Json.Encode 方法是否仍然导致问题。
    • 新解决方案解决了我的问题。但图像仍然没有改变。
    • 什么意思但图像仍然没有变化?你指的是当你点击一个按钮吗?
    • 点击按钮什么也不做。 ID 是正确的。我尝试将脚本放在标题和正文中。还是什么都没有
    猜你喜欢
    • 2016-01-01
    • 2023-03-21
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多