简体   繁体   English

MVC 5部分视图-模型混乱

[英]MVC 5 Partial View - Model confusion

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; 在SliderController中,我无法将var模型传递给局部视图,或者无法获得Iqueryable,ienumerable或错误;

LIST of Models.Slider is not assignable to type models.Slider Models.Slider的列表不可分配给类型模型。

Many thanks Simon 非常感谢西蒙

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: 您可以使用HtmlHelper.Action()方法从父视图中请求子操作:

<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. 当您以这种方式调用Html.Action方法时,结果视图(通常是部分视图)将在当前视图中就地呈现。 If you're familiar with PHP, it's somewhat similar to an include statement. 如果您熟悉PHP,它有点类似于include语句。

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 您正在尝试将列表分配给模型,但是您声明的模型不是列表类型,因此您想将其声明为列表类型,或者从结果中选择first或default

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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