简体   繁体   中英

Parse ViewBag with image content to Partial View

I wrote code that should return 2 banner files per language. In total there are 6 banner files (.jpg files). 2 of the banner files should be shown on the Index page. When the user is a French user, the 2 banner files for the French language should be shown. I have the following method in my home controller:

 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");
            }
        }
    }

The code in comments is code I used previously but did not work.

On the index page I just render the Partial View:

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

Partial View:

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

When running the website, the text "File1" and "File2" is shown instead. What did I do wrong here?

When creating the partial view _AdRotator on the return call and navigating to it, the following error message is shown:

Error message

Because of this error, I figured to use this instead as seen in the AdRotator() method:

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

Could this perhaps be the problem? I have looked around on stackoverflow and found similar questions but could not fix my problem.

I think you're rendering the partial view without calling the action in the controller.

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

Try: @Html.Action('AdRotator', 'Home')

That should call the AdRotator action, fill the ViewBag and display the images.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM