Eamonn
Eamonn

Reputation: 1320

Creating a ViewModel

I have two tables - Prm_Staff and Prm_Salutation - one of which holds staff names and a salutation ID, the other lists the salutations. Their models are such:

public class Prm_Staff
{
    public int ID { get; set; }
    public int SalutationID { get; set; }
    public string FName { get; set; }
    public string LName { get; set; }
    public bool Active { get; set; }

    //method to insert data
    public Prm_Staff(int id, int salID, string fname, string lname, bool active)
    {
        ID = id;
        SalutationID = salID;
        FName = fname;
        LName = lname;
        Active = active;
    }

    //parameterless constructor
    public Prm_Staff() { }
}

public class Prm_Salutation
{
    public int ID { get; set; }
    public string Desc { get; set; }
    public bool Active { get; set; }

    public Prm_Salutation(int id, string desc, Boolean active)
    {
        ID = id;
        Desc = desc;
        Active = active;
    }

    public Prm_Salutation() { }
}

I wish to have a View that has

I have built a view that fulfils the above, using a linq query to pass the Salutation data to the view via ViewData. I wish to know however how to establish a foreign key relationship, and then how to create a ViewModel that combines the necessary information and will pass it to the View in one go, as apparently that this is the proper way to achive what I'm doing.

I know I might come accross cheeky, but please, when answering use the simplest possible terms. I am self taught, so terminology you find second nature can be Vulcan to me. Also include any 'using: ...' statements, and be clear as to where any code examples are to be placed.

Upvotes: 0

Views: 319

Answers (1)

Guillermo Oramas R.
Guillermo Oramas R.

Reputation: 1303

A form to insert new staff members, with a dropdown of active salutations

I'd do it this way.

ViewModel:

public class Prm_Staff_View_Model
{
    public int ID { get; set; }
    public string FName { get; set; }
    public string LName { get; set; }
    public bool Active { get; set; }
    //The selected One
    public int SelectedSalutationID { get; set; }
    //The list with the salutations availables
    public List<Prm_Salutation> AvailableSalutations{get;set;} 

    public Prm_Staff() { }        
}

Controller GET method:

    // GET: /Create
    public ActionResult Create()
    {
        var staffCreateViewModel = new Prm_Staff_View_Model();
        staffCreateViewModel.AvailableSalutations = new List<Prm_Salutation>();
        //Here you get the salutations that want to display in the dropdown
        staffCreateViewModel.AvailableSalutations = context.getMySalutations();
        return View(staffCreateViewModel);
    } 

View: using the HTML extension DropDownListFor, we can do this:

@Html.DropDownListFor(model => model.SelectedSalutationID, Model.AvailableSalutations.Select(option => new SelectListItem {
    Text = Html.DisplayTextFor(_ => option.Desc ).ToString(), //Text shown in the DropList
    Value = option.ID.ToString(), //Value taken
    Selected = (Model != null) && (option.ID == Model.SelectedSalutationID) //If you're gonna edit the staff member, the selected salutation is the one that already has. 
}), "Choose...")

Controller POST method:

[HttpPost]
public ActionResult Create(Prm_Staff_View_Model viewModel)
{
   //Here you map your viewModel against the model and save it. 
   var myModel = new Prm_Staff();
   myModel.SalutationID = viewModel.SelectedSalutationID;
}

For mapping object I suggest the Nuget library AutoMapper , or you can do it manually. Let me know.

Upvotes: 1

Related Questions