ASP.NET MVC3:Dropdown中的默认值选择

时间:2012-02-20 11:00:51

标签: asp.net-mvc asp.net-mvc-3

我有一个名为Program的模型。每个节目都将有如下所述的电视节目表。

enum SchduleEnum { Daily = 0, Weekly, Monthly };

每个节目都可以有“新闻”,“电影”,“讨论”等类别。

在“计划安排”视图中,用户必须能够从下拉列表中选择计划。但是,计划下拉列表中应该有默认值。规则是默认时间表应为“每日”,如果它是“新闻”类别。如果类别为“电影”,则默认计划应为“每周”。如果类别为“讨论”,则默认计划应为“每月”。

我有以下代码,在计划下拉列表中显示空白作为默认值。我们如何根据上面定义的规则修改代码以显示所需的默认值?

基于MVC原则:The UI logic belongs in the view. Input logic belongs in the controller. Business logic belongs in the model.

  1. 我们如何快速实现?

  2. 我们如何达到标准方式(遵循上面列出的MVC原则)

  3. CODE

    public class Program
    {
        public int ProgramID { get; set; }
        public string ProgramName { get; set; }
        public int ScheduleID { get; set; }
        public string ProgramCategory { get; set; }
    }
    

    CONTROLLER

    namespace MyDefaultValueTEST.Controllers
    {
    public class MyProgramController : Controller
    {
        enum SchduleEnum { Daily = 0, Weekly, Monthly };
        List<Program> programList = new List<Program>()
                              {
                                new Program
                                {
                                    ProgramID = 1,ProgramName = "Program1",
                                    ProgramCategory = "News"
                                },
                                new Program
                                {
                                    ProgramID = 2,ProgramName = "Program2",
                                    ProgramCategory = "Movie"
                                },
                                new Program
                                {
                                    ProgramID = 3,ProgramName = "Program3",
                                    ProgramCategory = "Discussion"
                                }
    
                              };
    
    
    
        public ActionResult ScheduleProgram()
        {
            ViewBag.ScheudleEnum = GetSchduleSelectList();
            return View(programList);
        }
    
        public static SelectList GetSchduleSelectList()
        {
            Array values = Enum.GetValues(typeof(SchduleEnum));
            List<System.Web.UI.WebControls.ListItem> items = new List<System.Web.UI.WebControls.ListItem>(values.Length);
    
            foreach (var i in values)
            {
                items.Add(new System.Web.UI.WebControls.ListItem
                        {
                            Text = Enum.GetName(typeof(SchduleEnum), i),
                            Value = ((int)i).ToString()
                        }
                        );
            }
    
            return new SelectList(items);
        }
    
    
        }
    }
    

    查看

    @model IEnumerable<MyDefaultValueTEST.Program>
    
    @{
    ViewBag.Title = "ScheduleProgram";
    }
    
    <h2>ScheduleProgram</h2>
    
    
    @using (Html.BeginForm())
    {
    <table>
        <tr>
            <th style="border:1px solid Teal; background-color:Gray">
                ProgramName
            </th>
            <th style="border:1px solid Teal; background-color:Gray">
                ProgramCategory
            </th>
            <th style="border:1px solid Teal; background-color:Gray">  </th>
        </tr>
    
        @Html.EditorForModel()
    
    </table>
    
    <p>
        <input type="submit" value="Save Schedules" />
    </p>
    }
    

    EDITOR TEMPLATE(Program.cshtml)

    @model MyDefaultValueTEST.Program
    <tr>
    <td style="border:1px solid Teal">
        @Html.EditorFor(x => x.ProgramName)
    </td>
    <td style="border:1px solid Teal">
        @Html.EditorFor(x => x.ProgramCategory)
    </td>
    <td style="border:1px solid Teal">
        @Html.DropDownListFor(x => x.ScheduleID, (SelectList)ViewBag.ScheudleEnum, String.Empty)
    </td>
    <td>
        @Html.HiddenFor(x => x.ProgramID)
    </td>
    
    </tr>
    

    阅读

    1. C# mvc 3 using selectlist with selected value in view

    2. dropdownlist set selected value in MVC3 Razor

    3. MVC 3 Layout Page, Razor Template, and DropdownList

    4. DropdownListFor default value

    5. Html.dropdownlist not retaining the selected value

    6. Getting values from an asp.net mvc dropdownlist

    7. ASP.NET MVC ViewModel and DropDownList

    8. dropdown value null when using, viewmodel & modelbinder in asp.net mvc

    9. Support for optgroup in dropdownlist .NET MVC?

1 个答案:

答案 0 :(得分:1)

您可以将模型声明为

public class TestModel
{
    public SchduleEnum SelectedScheduleEnum { get; set; }
    public ProgramCatagory SelectedProgram { get; set; }

    public IEnumerable<SelectListItem> ScheduleEnums
    {
        get { return this.GetScheduleEnums(); }
    }

    public IEnumerable<SelectListItem> SelectedPrograms
    {
        get { return this.GetSelectedPrograms(); }
    }

    private IEnumerable<SelectListItem> GetSelectedPrograms()
    {
        List<SelectListItem> items=new List<SelectListItem>();
        Enum.GetNames(typeof(ProgramCatagory)).ToList().ForEach(s=>{
            bool IsSelected=((int)Enum.Parse(typeof(ProgramCatagory), s)).Equals((int)SelectedScheduleEnum);
            if(IsSelected)
                this.SelectedProgram = (ProgramCatagory)Enum.Parse(typeof(ProgramCatagory), s);
            items.Add(new SelectListItem()
                {
                    Text = s,
                    Value = s,
                    Selected = IsSelected
                });
        });
        return items;
    }

    private IEnumerable<SelectListItem> GetScheduleEnums()
    {
        List<SelectListItem> items = new List<SelectListItem>();
        Enum.GetNames(typeof(SchduleEnum)).ToList().ForEach(s =>
        {
            bool IsSelected = ((int)Enum.Parse(typeof(SchduleEnum),s)).Equals((int)SelectedProgram);
            if (IsSelected)
                this.SelectedScheduleEnum = (SchduleEnum)Enum.Parse(typeof(SchduleEnum), s);
            items.Add(new SelectListItem()
            {
                Text = s,
                Value = s,
                Selected = IsSelected
            });
        });
        return items;
    }

}
public enum SchduleEnum {
    Daily = 0, 
    Weekly,
    Monthly 
};
public enum ProgramCatagory
{
    News=0 ,
    Movie,
    Discussion
};

在控制器中,

public ActionResult Index()
    {

        TestModel model = new TestModel();
        model.SelectedScheduleEnum = SchduleEnum.Monthly;
        return View(model);
    }

在视图中,您可以调用,

<%:Html.DropDownListFor(m=>m.SelectedProgram,Model.SelectedPrograms)%>
<%:Html.DropDownListFor(m=>m.SelectedScheduleEnum,Model.ScheduleEnums) %>