MVC访问listboxFor的导航属性

时间:2013-12-14 12:32:44

标签: asp.net-mvc asp.net-mvc-3 model-view-controller

起初我应该说我是MVC的新手。  我有3个对象

 public partial class Magazine
{
    public Magazine()
    {
        this.NumberTitles = new HashSet<NumberTitle>();
    }

    public int Id { get; set; }
    public int MagYear { get; set; }
    public int MagNo { get; set; }
    public int MagSeason { get; set; }
    public string MagYear2 { get; set; }

    public virtual ICollection<NumberTitle> NumberTitles { get; set; }
}

public partial class NumberTitle
{
    public NumberTitle()
    {
        this.Articles = new HashSet<Article>();
    }

    public int Id { get; set; }
    public int MagazineId { get; set; }
    public int TitleId { get; set; }
    public int position { get; set; }

    public virtual ICollection<Article> Articles { get; set; }
    public virtual Magazine Magazine { get; set; }
    public virtual Title Title { get; set; }
}


public partial class Title
{
    public Title()
    {
        this.ChildrenTitle = new HashSet<Title>();
        this.NumberTitles = new HashSet<NumberTitle>();
    }

    public int Id { get; set; }
    public string TitleText { get; set; }
    public Nullable<int> ParentId { get; set; }

    public virtual ICollection<Title> ChildrenTitle { get; set; }
    public virtual Title ParentTitle { get; set; }
    public virtual ICollection<NumberTitle> NumberTitles { get; set; }
}

在视图中我想让TextBox显示杂志编号和2个列表框。一个显示所有可用标题和其他只选择该杂志编号的标题。所以我制作了视图模型

public class NumberTitleViewModel
{       
    public Magazine Magazine { get; set; }
    public List<NumberTitle> NumberTitles { get; set; }
}

这是在控制器中。如何获取指定MagazineId的标题列表

    public ActionResult EditTitle(int id)
    {
        Func<IQueryable<Magazine>, IOrderedQueryable<Magazine>> orderByFunc = null;
        Expression<Func<Magazine, bool>> filterExpr = null;
        if (id>0)
        {
            filterExpr = p => p.Id.Equals(id);
        }

        Magazine magazine = unitOfWork.MagazineRepository.Get(filter: filterExpr, orderBy: orderByFunc, includeProperties: "").SingleOrDefault();
        NumberTitleViewModel numberTitleViewMode = new NumberTitleViewModel();
        numberTitleViewMode.Magazine = magazine;
        Expression<Func<NumberTitle, bool>> filterExpr2 = null;
        if (id > 0)
        {
            filterExpr2 = p => p.MagazineId.Equals(id);
        }
        var numberTitles = unitOfWork.NumberTitleRepository.Get(filterExpr2, null, includeProperties: "Title").ToList();

        var titles = unitOfWork.TitleRepository.Get(null, null, "");
        numberTitleViewMode.NumberTitles = numberTitles; ///this part doesn't show the Titles. how should access the TitleName not Id
        ViewBag.titles = new SelectList(titles, "Id", "TitleText");
        return View("../Panel/Magazine/EditTitle", "_BasicLayout", numberTitleViewMode);
    }

1 个答案:

答案 0 :(得分:0)

不确定你的视图中有什么,但你应该有:

@using NameSpace.Models
@model NameSpace.Models.NumberTitleViewModel

然后你可以在你的视图中做这样的事情:

 foreach (NumberTitles item in   @Model)
    {
        <label>@item.Title.TitleText</label>
    }

不准确,但应该让你接近你需要的东西