-->

Building a DropDownList in the Model?

2019-09-19 07:19发布

问题:

I have a service layer that exposes a method, which returns me a List, called GetStates.

public List<StateObject> GetStates()

My State Object is just a custom object I have created:

public class StateObject
{
    public int stateId { get; set; }
    public string description { get; set; }
    public Boolean isDefault { get; set; }
}

In my models, I am trying to create a model that will be used for my display and modification screen of a task. One thing this will be used for is handling the display and selection of a Drop down box, which will give a list of States available for my Task. So, my model looks something like this (Removed properties we don't car about - it's a bit bigger than this:

public class TaskModifyModel
{
    public int stateId { get; set; }
    public string state { get; set; }

    public SelectList states { get; private set; }

    public TaskModifyModel()
    {

        states = new SelectList(
            (new ReferenceService().GetStates()),
            "stateId",
            "description",
            1);

    }
}

So, stateId holds the selected state, state holds the text description of the selected state. In the constructor, I am attempting to create a states SelectList for the view... and populate it.

In the view, I then try to display the Drop Down List:

@Html.DropDownListFor(m=>m.stateId, new SelectList(Model.states, "stateId", "description", Model.priorityId))

This is failing, dismally.

DataBinding: 'System.Web.Mvc.SelectListItem' does not contain a property with the name 'stateId'.

I have searched, and I thought I was doing this the right way, but the error says I am not.. clearly. :) Could someone guide me on why it's not working, and also, is this the right way to do things?

Edit: After assistance below, it's now working. If I am creating a new task (taskId==0), then I have to get the default value of the dropdown, as stored in my database.... So, is this clean? This is my working constructor for the object:

public TaskModifyModel() { var referenceService = new ReferenceService(); var p = referenceService.GetPriorities(); var s = referenceService.GetStates();

    var defaultP = (from a in p where a.isDefault select a).FirstOrDefault();
    var defaultS = (from a in s where a.isDefault select a).FirstOrDefault();

    priorities = new SelectList(
        (p),
        "priorityId",
        "description"
        );
    priorityId = taskId == 0 ? defaultP.priorityId : priorityId;

    states = new SelectList(
        s,
        "stateId",
        "description");
    stateId = taskId == 0 ? defaultS.stateId : stateId;

}

Is it OK?

回答1:

Your public SelectList states { get; private set; } is already a SelectList so you don't need to cast it again in your View.

Try this instead:

@Html.DropDownListFor(m=>m.stateId, Model.states)

And in your ViewModel, remove the parameter "SelectedValue". The @Html.DropDownListFor will initialize the dropdown to the right value.

public class TaskModifyModel
{
    public int stateId { get; set; }
    public string state { get; set; }

    public SelectList states { get; private set; }

    public TaskModifyModel()
    {

        states = new SelectList(
            (new ReferenceService().GetStates()),
            "stateId",
            "description");
    }
}