Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Thanks to Darin. I have accepted his answer. The complete solution is posted for the benefit of others reading this.</p> <p>Note: EditorTemplate helped to avoid foreach loop and helped in proper object binding.</p> <p>Note: The EditorTemplate should be placed in proper folder and the filename should be based on convention.</p> <p>//Model</p> <pre><code>namespace MyProgramRatingApp { public enum RatingEnum { Poor = 0, Neutral, Good, Excellent }; public class Program { public int ProgramID { get; set; } public string ProgramName { get; set; } public RatingEnum Rating { get; set; } public string ProgramCategory { get; set; } } } </code></pre> <p>//Controller</p> <pre><code>using System; using System.Collections.Generic; using System.Web.Mvc; namespace MyProgramRatingApp.Controllers { public class ProgramController : Controller { List&lt;Program&gt; programList = new List&lt;Program&gt;() { new Program { ProgramID = 1,ProgramName = "Program1", ProgramCategory = "A" }, new Program { ProgramID = 2,ProgramName = "Program2", ProgramCategory = "B" }, new Program { ProgramID = 3,ProgramName = "Program3", ProgramCategory = "A" } }; // GET: /Program/ public ActionResult CastVote() { ViewBag.RatingEnum = GetRstingSelectList(); return View(programList); } // POST: /StoreManager/Create [HttpPost] public ActionResult CastVote(IEnumerable&lt;Program&gt; theProgramList) { if (ModelState.IsValid) { //Save the book in DB first and then redirectToAction. return RedirectToAction("CastVote"); } return View(theProgramList); } public static SelectList GetRstingSelectList() { Array values = Enum.GetValues(typeof(RatingEnum)); 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(RatingEnum), i), Value = ((int)i).ToString() } ); } return new SelectList(items); } } } </code></pre> <p>View(CastVote.cshtml)</p> <pre><code>@model IEnumerable&lt;MyProgramRatingApp.Program&gt; @{ ViewBag.Title = "CastVote"; } &lt;h2&gt;CastVote&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="Cast Vote" /&gt; &lt;/p&gt; } </code></pre> <p>EditorTemplate(Program.cshtml)</p> <pre><code>@model MyProgramRatingApp.Program &lt;tr&gt; &lt;td style="border:1px solid Teal"&gt; @Html.DisplayFor(x =&gt; x.ProgramName) @Html.HiddenFor(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.Rating, (SelectList)ViewBag.RatingEnum, String.Empty) &lt;/td&gt; &lt;td&gt; @Html.HiddenFor(x =&gt; x.ProgramID) &lt;/td&gt; &lt;/tr&gt; </code></pre>
    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.
    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