簡體   English   中英

將具有圖像內容的ViewBag解析為Partial View

[英]Parse ViewBag with image content to Partial View

我編寫的代碼應返回每種語言2個標題文件。 總共有6個標題文件(.jpg文件)。 標語文件中的2個應顯示在“索引”頁面上。 如果用戶是法語用戶,則應顯示法語的2個橫幅文件。 我的家庭控制器中有以下方法:

 public ActionResult AdRotator()
    {
        //nog opvullen met banners
        var userlanguage = Request.UserLanguages;


        if (userlanguage.Equals("nl-nl || nl-be"))
        {
            ViewBag.File1 = Server.MapPath("~") + "Content/Images/Banners/Banner_NL.jpg";
            ViewBag.File2 = Server.MapPath("~") + "Content/Images/Banners/Banner2_NL.jpg";
            // ViewBag.File1 =  Url.Content("~/Content/Images/Banners/Banner_NL.jpg");
            // ViewBag.File2 = Url.Content("~/Content/Images/Banners/Banner2_NL.jpg");
            // ViewBag.File1 = File("~/Content/images/Banners/Banner_NL.jpg", "image/jpg");

            //view meegeven omdat aangemaakte partialview anders niet gevonden wordt na creatie
            return PartialView("~/Views/Home/_AdRotator.cshtml");

        }
        else
        {
            if(userlanguage.Equals("fr-fr||fr-lu"))
            {
                ViewBag.File1 = Server.MapPath("~") + "Content/Images/Banners/Banner_FR.jpg";
                ViewBag.File2 = Server.MapPath("~") + "Content/Images/Banners/Banner2_FR.jpg";
                return PartialView("~/Views/Home/_AdRotator.cshtml");
            }
            else
            {
                ViewBag.File1 = Server.MapPath("~") + ("~/Content/Images/Banners/Banner_EN.jpg");
                ViewBag.File2 = Server.MapPath("~") + ("~/Content/Images/Banners/Banner2_EN.jpg");
                return PartialView("~/Views/Home/_AdRotator.cshtml");
            }
        }
    }

注釋中的代碼是我以前使用過的代碼,但是沒有用。

在索引頁面上,我只渲染部分視圖:

@Html.Partial("~/Views/Home/_AdRotator.cshtml");

部分視圖:

<img src="@ViewBag.File1" alt="File1" /> <img src="@ViewBag.File2" alt="File2" />

運行網站時,將顯示文本“ File1”和“ File2”。 我在這里做錯了什么?

在回叫上創建局部視圖_AdRotator並導航至該局部視圖時,會顯示以下錯誤消息:

錯誤信息

由於存在此錯誤,因此我想改用它,如AdRotator()方法所示:

return PartialView("~/Views/Home/_AdRotator.cshtml");

可能是問題所在嗎? 我環顧了stackoverflow,發現了類似的問題,但無法解決我的問題。

我認為您正在渲染部分視圖,而無需在控制器中調用操作。

代替@Html.Partial("~/Views/Home/_AdRotator.cshtml")

嘗試: @Html.Action('AdRotator', 'Home')

這應該調用AdRotator操作,填充ViewBag並顯示圖像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM