简体   繁体   English

MVC5:迭代 ViewModel

[英]MVC5: Iterate ViewModel

In my viewmodel, along with some other properties, I have this:在我的视图模型中,连同其他一些属性,我有这个:

public int AuthorStatus {get; set;}

public string AuthorStatusLabelClass
{
    get
    {
        switch (AuthorStatus)
        {
            case 1:
                return "label-success";
            case 0:
                return "label-danger";
            case 2:
                return "label-warning";
            default:
                return "label-default";
        }
    }
    set
    {

    }
}

public IQueryable<VM_Authors> AuthorList { get; set; }

How can I access the AuthorStatusLabelClass in my controller;如何访问控制器中的 AuthorStatusLabelClass;

    var vm = new ViewModels.VM_Authors();
    vm.Categories = new SelectList(db.AuthorCategories, "CategoryID", "CategoryName");
    vm.AuthorList = (from a in db.Authors
                     select new ViewModels.VM_Authors
                     {
                         FirstName = a.FirstName,
                         LastName = a.LastName,
                         Email = a.Email,
                         AuthorStatusLabelClass = ####HOW_SHOULD_I_RETRIEVE_IT_HERE####

So that I can use it in my view like this:这样我就可以像这样在我的视图中使用它:

        @foreach (var item in Model.AuthorList)
        {
        <tr>
            <td><span class="label @item.AuthorStatusLabelClass">@item.AuthorStatus</span></td>
            <td><img src="" class="img-circle" /></td>
            <td><span class="text-semibold">@item.FirstName @item.LastName</span></td>
            <td>@item.SelectedCategoryID</td>
            <td></td>
        </tr>
        }

May be my whole approach is different?可能是我的整个方法不同吗? Any help would be appreciated!任何帮助,将不胜感激!

Edit:编辑:

Since you mention that AuthorStatusLabelClass is not one of your database's table's column and that it entirely depends on your AuthorStatus , then you do not need to assign it at all.由于您提到AuthorStatusLabelClass不是数据库表的列之一并且它完全取决于您的AuthorStatus ,因此您根本不需要分配它。 Simply do:简单地做:

select new ViewModels.VM_Authors
 {
     FirstName = a.FirstName,
     LastName = a.LastName,
     Email = a.Email,
     AuthorStatus = a.AuthorStatus //assign this

And when you get your AuthorStatusLabelClass in your View , just get it directly and your assigned AuthorStatus above will take care of the return of your AuthorStatusLabelClass in the View .当您在View获取AuthorStatusLabelClass时,只需直接获取它,上面分配的AuthorStatus将处理您的AuthorStatusLabelClassView中的返回。

Original:原来的:

You do not seem to set a setter in your AuthorStatusLabelClass , thus you cannot do this:您似乎没有在AuthorStatusLabelClass设置setter ,因此您不能这样做:

select new ViewModels.VM_Authors
 {
     FirstName = a.FirstName,
     LastName = a.LastName,
     Email = a.Email,
     AuthorStatusLabelClass = //This is a set ####HOW_SHOULD_I_RETRIEVE_IT_HERE####

AuthorStatusLabelClass with setter may look like this:带有setter AuthorStatusLabelClass可能如下所示:

private string _authorStatusLabelClass = "";
public string AuthorStatusLabelClass
{
    get
    {
        switch (AuthorStatus)
        {
            case 1:
                _authorStatusLabelClass = "label-success";
            case 0:
                _authorStatusLabelClass = "label-danger";
            case 2:
                _authorStatusLabelClass = "label-warning";
            default:
                _authorStatusLabelClass = "label-default";
        }
        return _authorStatusLabelClass;
    }
    set
    {
        _authorStatusLabelClass = value;
    }
}

Also, since you require your AuthorStatus to be set to a number first before you could retrieve your AuthorStatusLabelClass , you probably cannot really get your AuthorStatusLabelClass correctly without getting AuthorStatus .此外,由于你需要你的AuthorStatus设置为若干第一之前,你可以检索你AuthorStatusLabelClass ,你可能无法真正得到您的AuthorStatusLabelClass正确没有得到AuthorStatus What you probably need in the class AuthorStatusLabelClass is an indexer :您在AuthorStatusLabelClass类中可能需要的是一个indexer

public AuthorStatusLabelClass this[int index] { //thus you could give index as an input for this instance
  get {
    switch (index)
    {
        case 1:
            return "label-success";
        case 0:
            return "label-danger";
        case 2:
            return "label-warning";
        default:
            return "label-default";
    }
  }       
}

And use it like this:并像这样使用它:

vm.AuthorList = (from a in db.Authors
                 select new ViewModels.VM_Authors
                 {
                     FirstName = a.FirstName,
                     LastName = a.LastName,
                     Email = a.Email,
                     AuthorStatusLabelClass = a.AuthorStatusLabelClass[a.AuthorStatus]

As I can see your AuthorStatusLabelClass has getter and an empty setter.正如我所看到的,你的AuthorStatusLabelClass有 getter 和一个空的 setter。 Getter is dependent on AuthorStatus property. Getter 依赖于AuthorStatus属性。 All you have to do is to set up AuthorStatus property properly.您所要做的就是正确设置AuthorStatus属性。

select new ViewModels.VM_Authors
 {
     FirstName = a.FirstName,
     LastName = a.LastName,
     Email = a.Email,
     AuthorStatus = ### SETUP status and don't worry about AuthorStatusLabelClass ###

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

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