Building a DropDownList in the Model?

偶尔善良 提交于 2020-01-06 15:07:49

问题


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");
    }
}


来源:https://stackoverflow.com/questions/12118706/building-a-dropdownlist-in-the-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!