0
votes

I have a model called "Slider"

using System;
using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;

namespace BarMotors.Models
{
public class Slider
{
    public virtual int Id { get; set; }
    public virtual string Photo{ get; set; }
    public virtual string LeftText{ get; set; }
    public virtual string RightText { get; set; }
    public virtual int SortOrder{ get; set; }
    public virtual DateTime CreatedAt { get; set; }
    public virtual DateTime? UpdatedAt { get; set; }
    public virtual DateTime? DeletedAt { get; set; }

    public virtual bool IsDeleted
    {
        get { return DeletedAt != null; }
    }
}

public class SliderMap : ClassMapping<Slider>
{
    public SliderMap()
    {
        Table("Sliders");
        Id(x => x.Id, x => x.Generator(Generators.Identity));
        Property(x => x.Photo, x => x.NotNullable(true));
        Property(x => x.LeftText);
        Property(x => x.RightText);
        Property(x => x.SortOrder, x => x.NotNullable(true));
    }
}
}

I also (now) have this controller;

using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using BarMotors.Models;
using NHibernate.Linq;

namespace BarMotors.Controllers
{
    public class SliderController : Controller
    {
        [ChildActionOnly]
        public ActionResult Sliders()
        {
            List<Slider> model;
            new List<Models.Slider>();
            model = Database.Session.Query<Slider>().Where(s => s.DeletedAt == null).OrderBy(x=>x.SortOrder).ToList();

            return PartialView("_HomeSlider", model);
        }
    }
}

MAIN view

@{
    ViewBag.Title = "Homepage";
}
<h1>Homepage</h1>
@Html.Action("Sliders", "Slider")

Partial view

@model BarMotors.Models.Slider

@{
    Layout = null;
}
//do something in loop of Slider

In SliderController I cannot pass var model to the partial view I either get Iqueryable, ienumerable or errors along the lines of;

LIST of Models.Slider is not assignable to type models.Slider

Many thanks Simon

2

2 Answers

1
votes

Your are trying to assign the list to a the model but you have declared a model which is not list type so you either want to declare it as a list type or select firstordefault from the result

public class MyController : Controller
{
    /* ... your code ... */

    [ChildActionOnly]
    public ActionResult Sliders()
    {
        var model = new Models.Slider();

        model = Database.Session.Query<Slider>().Where(s => s.DeletedAt == null).OrderBy(x=>x.SortOrder).ToList().FirstOrDefault();

        return PartialView("your-view-name", model);
    }
}

Otherwise

public class MyController : Controller
{
    /* ... your code ... */

    [ChildActionOnly]
    public ActionResult Sliders()
    {
        var model = new List<Models.Slider>();

        model = Database.Session.Query<Slider>().Where(s => s.DeletedAt == null).OrderBy(x=>x.SortOrder).ToList();

        return PartialView("your-view-name", model);
    }
}

And in partial View you have to change it to

@model List<BarMotors.Models.Slider>

@{
    Layout = null;
}
1
votes

If your slider has nothing to do with the parent model, you will probably want to use a child action.

Your controller would have an action like so:

public class MyController : Controller
{
    /* ... your code ... */

    [ChildActionOnly]
    public ActionResult Sliders()
    {
        var model = new Models.Slider();

        /* ... populate model ... */

        return PartialView("your-view-name", model);
    }
}

You would use the HtmlHelper.Action() method to request the child action from within the parent view:

<div>
    <!-- more view markup ... -->

    @Html.Action("sliders", "my")

</div>

When you call the Html.Action method in this way, the resulting view (normally a partial view) is rendered in-place in the current view. If you're familiar with PHP, it's somewhat similar to an include statement.