Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Lambda or Linq to create Model of SelectList with Selected item for razor View in MVC5

Tags:

I'm using EF code first and MVC5 C# for my web application. I've created a db table that contains master definitions for a couple dozen settings that are used to specify input and output parameters throughout the application.

I've also created a table that keys on user ID. Each user that gets created is given default settings that can be changed at any time using a User Preferences (razor) View page.

Now I am trying to code the controller actions for this View page. Most all of these user preferences will require selection from a dropdown list, but this is where it's confusing for me.

// UOM Master Table
public class UOMMaster
{
    public int Id { get; set; }
    public string MeasureId { get; set; }         // Text used as labels
    public double SortOrder { get; set; }         // User preferences sort order
    public string UOMId { get; set; }             // UOM abbreviation
    public bool IsUserPreference { get; set; }    // true if is a stored user preference
}

The master table data above may look like:

1,   "AbsolutePress",   1,   "psi",    true
2,   "AbsolutePress",   1,   "kPaa",   true
3,   "FluidFlow",       2,   "GPM",    true
4,   "FluidFlow",       2,   "m^3/Hr", true
5,   "FluidFlow",       2,   "l/s",    true

etc, etc...

The custom Identity with an ICollection containing :

public class ApplicationUser : IdentityUser
{
    // Default Units of Measure
    public virtual ICollection<AspNetUserUOMDefault> UOMDefaults { get; set; }
}

The preferred user defaults are stored in a table keyed on Identity's UserId:

public class AspNetUserUOMDefault
{
    [Key]
    public virtual ApplicationUser UserProfile { get; set; }

    public int Id { get; set; }
    public string MeasureId { get; set; }
    public string PreferredUomId { get; set; }
}

Using the tables above, I need to create a razor View that displays dropdown lists to display the user's preferred setting:

                  +--------------+---+
AbsolutePress:    | psi          | V |     <-- Dropdown list containing 
                  +--------------+---+         all options available for
                  | psi              |         AbsolutePress as defined in
                  | kPaa             |         master table, with user's
                  +------------------+         preferred setting selected.

FluidFlow:        +--------------+---+
                  | m^3/Hr       | V |     <-- Dropdown list containing
                  +--------------+---+         all options available for
                  | GPM              |         FluidFlow as defined in
                  | m^3/Hr           |         master table, with user's
                  | l/s              |         preferred setting selected.
                  +------------------+

etc, etc...

The GET Controller Action confuses me. I just don't have an idea on how to form the Model returned to the razor View, but it needs to be ordered by SortOrder (ascending) and contain an IList/ICollection of SelectList (perhaps??) containing the following (I'm not sure how to do this):

UOMPreferences
    MeasureId
    optionsList
        UomId
        UomId (Selected)
        UomId
UOMPreferences
    MeasureId
    optionsList
        UomId (Selected)
        UomId

   etc, etc..

The View template needs to display the contents of the Model using dropdown lists in SortOrder order. I envision this being dynamic so that a preference could be added to the database table without any changes required to either the Controller Actions or razor View template.

In the View there would be a foreach (I'm not sure how to do this):

@foreach (var preference in Model.UOMPreferences)
{
<div class="form-group">
    @Html.LabelFor(preference.MeasureId, new { @class = "col-md-3 control-label" })
    <div class="col-md-4">
        @Html.DropDownListFor(
            preference.MeasureId,
            new SelectList(preference.optionsList, "Value", "Text"), 
            new { @class = "form-control" })
    </div>
</div>
}

Does anyone have the experience and time to provide me with code snippet suggestions on how I can accomplish this? Probably my biggest confusion is in the Controller Actions themselves (GET and POST). I don't really care if the Model is created with Lambda or Linq, but I just can't envision how this can be done - and I'm sure it can be. My database models can likely be improved, though I think they will work?