Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC3: Default value selection in Dropdown
    primarykey
    data
    text
    <p>I have a model named Program. Each program will have a telecast schedule as described below. </p> <pre><code>enum SchduleEnum { Daily = 0, Weekly, Monthly }; </code></pre> <p>Each program can have a category like “News” , “Movie”, “Discussion”. </p> <p>In the Program Scheduling view, users must be able to select the schedule from a dropdown. However, there should be default values in the schedule dropdown. The rule is that the default schedule should be “Daily” if it is “news” category. The default schedule should be “Weekly” if the category is “Movie”. The default schedule should be “Monthly” if category is “Discussion”.</p> <p>I have the following code which shows blank as default value in schedule dropdown. How do we modify the code to show required default values based on the rules defined above?</p> <p>Based on MVC principle: <code>The UI logic belongs in the view. Input logic belongs in the controller. Business logic belongs in the model.</code></p> <ol> <li><p>How do we achieve it in quick way?</p></li> <li><p>How do we achieve it standard way (following MVC principle listed above)</p></li> </ol> <p>CODE</p> <pre><code>public class Program { public int ProgramID { get; set; } public string ProgramName { get; set; } public int ScheduleID { get; set; } public string ProgramCategory { get; set; } } </code></pre> <p>CONTROLLER</p> <pre><code>namespace MyDefaultValueTEST.Controllers { public class MyProgramController : Controller { enum SchduleEnum { Daily = 0, Weekly, Monthly }; List&lt;Program&gt; programList = new List&lt;Program&gt;() { 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&lt;System.Web.UI.WebControls.ListItem&gt; items = new List&lt;System.Web.UI.WebControls.ListItem&gt;(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); } } } </code></pre> <p>VIEW</p> <pre><code>@model IEnumerable&lt;MyDefaultValueTEST.Program&gt; @{ ViewBag.Title = "ScheduleProgram"; } &lt;h2&gt;ScheduleProgram&lt;/h2&gt; @using (Html.BeginForm()) { &lt;table&gt; &lt;tr&gt; &lt;th style="border:1px solid Teal; background-color:Gray"&gt; ProgramName &lt;/th&gt; &lt;th style="border:1px solid Teal; background-color:Gray"&gt; ProgramCategory &lt;/th&gt; &lt;th style="border:1px solid Teal; background-color:Gray"&gt; &lt;/th&gt; &lt;/tr&gt; @Html.EditorForModel() &lt;/table&gt; &lt;p&gt; &lt;input type="submit" value="Save Schedules" /&gt; &lt;/p&gt; } </code></pre> <p>EDITOR TEMPLATE(Program.cshtml)</p> <pre><code>@model MyDefaultValueTEST.Program &lt;tr&gt; &lt;td style="border:1px solid Teal"&gt; @Html.EditorFor(x =&gt; x.ProgramName) &lt;/td&gt; &lt;td style="border:1px solid Teal"&gt; @Html.EditorFor(x =&gt; x.ProgramCategory) &lt;/td&gt; &lt;td style="border:1px solid Teal"&gt; @Html.DropDownListFor(x =&gt; x.ScheduleID, (SelectList)ViewBag.ScheudleEnum, String.Empty) &lt;/td&gt; &lt;td&gt; @Html.HiddenFor(x =&gt; x.ProgramID) &lt;/td&gt; &lt;/tr&gt; </code></pre> <p><strong>READING</strong></p> <ol> <li><p><a href="https://stackoverflow.com/questions/4579598/c-sharp-mvc-3-using-selectlist-with-selected-value-in-view">C# mvc 3 using selectlist with selected value in view</a></p></li> <li><p><a href="https://stackoverflow.com/questions/6807256/dropdownlist-set-selected-value-in-mvc3-razor">dropdownlist set selected value in MVC3 Razor</a></p></li> <li><p><a href="https://stackoverflow.com/questions/4674033/mvc-3-layout-page-razor-template-and-dropdownlist">MVC 3 Layout Page, Razor Template, and DropdownList</a></p></li> <li><p><a href="https://stackoverflow.com/questions/7229626/dropdownlistfor-default-value">DropdownListFor default value</a></p></li> <li><p><a href="https://stackoverflow.com/questions/8081335/html-dropdownlist-not-retaining-the-selected-value">Html.dropdownlist not retaining the selected value</a></p></li> <li><p><a href="https://stackoverflow.com/questions/1398196/getting-values-from-an-asp-net-mvc-dropdownlist">Getting values from an asp.net mvc dropdownlist</a></p></li> <li><p><a href="https://stackoverflow.com/questions/2167166/asp-net-mvc-viewmodel-and-dropdownlist">ASP.NET MVC ViewModel and DropDownList</a></p></li> <li><p><a href="https://stackoverflow.com/questions/1155061/dropdown-value-null-when-using-viewmodel-modelbinder-in-asp-net-mvc">dropdown value null when using, viewmodel &amp; modelbinder in asp.net mvc</a> </p></li> <li><p><a href="https://stackoverflow.com/questions/607188/support-for-optgroup-in-dropdownlist-net-mvc">Support for optgroup in dropdownlist .NET MVC?</a></p></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload