简体   繁体   中英

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;

    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. 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 .

Original:

You do not seem to set a setter in your AuthorStatusLabelClass , thus you cannot do this:

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:

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 . What you probably need in the class AuthorStatusLabelClass is an 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. Getter is dependent on AuthorStatus property. All you have to do is to set up AuthorStatus property properly.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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