【问题标题】:Display partial view in a main View在主视图中显示局部视图
【发布时间】:2014-05-07 22:00:54
【问题描述】:

我需要知道如何在主视图的特定区域显示部分视图,这是我的源代码 主视图:cshtml

<table  align="left" border="5px" cellspacing="2px" style="border:2px solid black">
<tr>       
    <table>
        <tr>
            <td style="border:2px solid black">
                <div style="background-color: #fff; border: 6px solid #ccc; height: 700px; width: 500px; margin: 20px; padding: 20px;" align="center">Select Baseline Image
                    <input type="button" value="Upload" align="right">
                    <div style="background-color: #fff; border: 6px solid #ccc; height: 500px; width: 300px; margin: 20px; padding: 20px;" align="center">@Html.Partial("_DisplayImage")</div>
                    </div>
                <form method="POST">
                    <input id="leftUrlTB" name="URL" type="Text" style="background-color: #fff; border: 6px solid #ccc"/>
                    <input id="leftBrowser" name="leftBrowser" value="Firefox" type="radio" />Firefox<br>
                    <input id="leftBrowser" name="leftBrowser" value="Chrome" type="radio" />Chrome<br>
                    <input id="leftBrowser" name="leftBrowser" value="Internet Explorer" type="radio" />Internet Explorer<br>
                    <input type="submit" value="Capture" align="right">
                </form>
            </td>                

            <td style="border:2px solid black">
                <div style="background-color: #fff; border: 6px solid #ccc; height: 700px; width: 500px; margin: 20px; padding: 20px;" align="center">Select Comparison Image
                    <input type="button" value="Upload" align="right">
                    <div style="background-color: #fff; border: 6px solid #ccc; height: 500px; width: 300px; margin: 20px; padding: 20px;" align="center"></div></div>

                <form method="POST">
                    <input id="rightUrlTB" name="URL" type="Text" style="background-color: #fff; border: 6px solid #ccc"/>
                    <input id="rightBrowser" name="leftBrowser" value="Firefox" type="radio" />Firefox<br>
                    <input id="rightBrowser" name="leftBrowser" value="Chrome" type="radio" />Chrome<br>
                    <input id="rightBrowser" name="leftBrowser" value="Internet Explorer" type="radio" />Internet Explorer<br>
                    <input type="submit" value="Capture" align="right">
                </form>

            </td>
        </tr>    

    </table>

</tr>

<tr>
    <td  >
        <div style="background-color: #fff; border: 6px solid #ccc; height: 700px; width: 1100px; margin: 20px; padding: 20px;" align="center"></div>
    </td>     

部分视图代码是这个:

<img src="@Url.Action("WebAppView")" />

我得到了那些控制器:

public ActionResult WebAppView()
    {
        return View();
    }

  [HttpPost]
    public ActionResult WebAppView(string url, string selectedBrowserRadio)
    {
      selectedBrowserRadio = Request.Form["leftBrowser"];
      selectedBrowserRadio = selectedBrowserRadio.Replace(" ", string.Empty);
      var selectedBrowser = (Browser)Enum.Parse(typeof(Browser), selectedBrowserRadio, true);
      var bmp= ScreenShotter.TakeScreenShot(selectedBrowser, url);
      var converter = new ImageConverter();
      var bmpArray = (byte[])converter.ConvertTo(bmp, typeof(byte[]));
      return File(bmpArray,"image/png");
    }


  public ViewResult _DisplayImage()
  {
      return View("_DisplayImage");
  }

我的问题是返回的图像显示在整个视图中,而不是在我指定的 div 区域中。我需要在左侧 div 区域中渲染它 谢谢你

【问题讨论】:

    标签: asp.net-mvc-4 partial-views asp.net-mvc-partialview


    【解决方案1】:

    尝试改变

      public ActionResult WebAppView()
      {
         return View();
      }
    

    进入

    public ActionResult WebAppView()
        {
            return PartialView();
        }
    

    【讨论】:

    • 不幸的是,这不起作用,它与我之前得到的结果相同,w 整个视图显示图像,而不是在表格的左侧区域显示它
    • 试试@KevDevMan 所说的,也许你的问题出在 _DisplaImage 而不是 。如果还是不能解决你的问题,请问WebAppView的post方法是做什么用的? 只会访问get 版本,不会访问post 版本。因此,也许您期望 post 方法触发(通过您希望获取文件的外观),但是您从 get 获得了一个 html 视图
    • 其实我想做的是将屏幕截图作为给定网页的 Bitmap 对象,所以 Http post 方法将返回一个文件给我,我将使用 中的这个文件,然后我必须在主视图的特定区域显示这个图像
    • 试着把它放在你的 _DisplayImage @{ Layout = null; } 看看有没有帮助
    • 可能问题出在 WebAppView() 视图中,您能显示该操作的视图代码吗?
    【解决方案2】:

    @Html.Partial("_DisplayImage") 应该直接调用局部视图而不是通过控制器。

    您的意思是使用@Html.RenderAction() 来调用Action 方法吗?如果是这样,您的操作方法将返回 view("_DisplayImage") 结果,而不是应该返回的 PartialView("_DisplayImage")。

    【讨论】:

    • 抱歉,怎么回事? @Html.Partial("_DisplayImage") 如何直接调用局部视图而不通过控制器。?我对 Asp.net mvc 很陌生
    • Partial 就是这样做的,看看它的参数列表'stringPartialViewName'。 RenderAction() 将采用控制器和动作参数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多