user1983128
user1983128

Reputation:

How do I call a partial view with List collection in a view

i want display a Slider within the webpage slider page is partial

   @Html.Partial("~/Views/slider/slider.cshtml", new List<Mvc_baker.Areas.admin.Models.slidShow>())

this code view slider

<div id="amazingslider-wrapper-1" style="float:left;  display:block;position:relative;max-width:605px;margin:5px 5px 60px;">
<div id="amazingslider-1" style=" display:block;position:relative;margin:0 auto;">

    @{
        string imageBase64 = "";
        string imageSrc = "";
    }
    @*@if (Model != null)*@


    @foreach (var item in Model)
    {
        <ul class="amazingslider-slides" style="display:none;">
            <li>

                @*<img src="~/Content/Images/main/sliderengine/main2.png" alt="main2" />*@
                imageBase64 = Convert.ToBase64String(@item.FImage);
                imageSrc = string.Format("data:image/gif;base64,{0}", imageBase64);
                @if (imageSrc != "")
                {
                    <img src="@imageSrc" class="info" width="100" height="50" />
                }

            </li>
        </ul>

    }

but when trace code view The code inside the foreach will not run iam think problem is new List() when replace with this code i am see error

Compiler Error Message: CS0118: 'System.Collections.Generic.IEnumerable' is a 'type' but is used like a 'variable

 @Html.Partial("~/Views/slider/slider.cshtml", new IEnumerable<Mvc_baker.Areas.admin.Models.slidShow>)

how sloved this problem

Upvotes: 0

Views: 1390

Answers (1)

hutchonoid
hutchonoid

Reputation: 33326

If you are just wanting to get this to compile change it to this:

@Html.Partial("~/Views/slider/slider.cshtml", 
   new List<Mvc_baker.Areas.admin.Models.slidShow>())

This changes it to a List class which implements the IEnumerable interface.

I guess at a later point you will want to pass this in as a model property like this:

@Html.Partial("~/Views/slider/slider.cshtml", Model.SlidesToShow)

This would require you adding this property to your model:

public IEnumerable<Mvc_baker.Areas.admin.Models.slidShow> SlidesToShow { get; set; }

Then populating it from your controller and returning it to the view like this (assuming property of FImage is a string without seeing your model).

model.SlidesToShow = new List<Mvc_baker.Areas.admin.Models.slidShow>() 
 { 
       new slidShow { FImage = "123"} 
 };
return View(model);

Upvotes: 1

Related Questions