0
votes

I am using SelectList to populate dropdownlists in my view. It works for the Create and Edit views to store ID value data in a table. How do I retrieve the 'Name' value to display in a Details view?

Model

Public Class Employee {
 [Key]
 public int ID { get; set;}
 public string UserName {get; set; }
 public byte Gender { get; set; }
}

ViewModel

public class EmployeeEditViewModel {
 public int ID { get; set; }
 public string UserName { get; set; }
 public SelectList GenderList { get; set; }

 public EmployeeEditViewModel () {
        GenderList = CommonHelper.GenderList(null);
    }
}

Helper

 public static SelectList GenderList(object selected)
    {
        return new SelectList(new[]
        {
            new { Value = 0, Name = "Male" },
            new { Value = 1, Name = "Female" }
            }
            , "Value", "Name", selected);
    }

Edit View

@model Models.ViewModel.EmployeeEditViewModel
@using (Html.BeginForm()) {
@Html.HiddenFor(model => model.ID)
<div class="form-group">
    @Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(model => model.GenderList, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownListFor(model => model.Gender, Model.GenderList, "- Select -", new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.GenderList, "", new { @class = "text-danger" })
    </div>
</div>
}

Controller

[HttpPost]
    public ActionResult CreateEmployee(EmployeeEditViewModel emProfile)
    {
        try
        {
            if (ModelState.IsValid)
            {
                Employee newUser = new Employee();
                newUser.UserName = emProfile.UserName;
                newUser.Gender = emProfile.Gender;

                userRepository.Add(newUser);
                userRepository.SaveChanges();
                return RedirectToAction("Index");
            }
        }
        catch (Exception ex)
        { }            
        return View(emProfile);
    }

So far it works great, I am able to create, edit Employee records and 1 or 0 is stored in the table for the gender. But when I want to display the employee data in a details view how do I get the text 'Male' or 'Female'?

1
Create a DetailsViewModel. You don't need the list, just have a Gender Description that you populate in the controller action.Steve Greene
Use an Enum since you have integer values for male and female, that way the enum maps correctly without further model creationBosco

1 Answers

0
votes

I ended up creating a helper method to retrieve the text.

public static string GetTextFromSelectList(int id, string listName)
    {
        string selectedText = string.Empty;
        SelectListItem selectedItem;

        switch (listName)
        {
            case "Gender":
                selectedItem = Helper.CommonHelper.GenderList(null).FirstOrDefault(x => x.Value == id.ToString());
                selectedText = selectedItem == null ? null : selectedItem.Text;
                break;                
            default:
                selectedText = null;
                break;
        }
        return selectedText;
    }