Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Example from MVC 4 for dropdownlist validation on Submit using Dataannotation and ViewBag (less line of code)</p> <p>Models:</p> <pre><code>namespace Project.Models { public class EmployeeReferral : Person { public int EmployeeReferralId { get; set; } //Company District //List [Required(ErrorMessage = "Required.")] [Display(Name = "Employee District:")] public int? DistrictId { get; set; } public virtual District District { get; set; } } namespace Project.Models { public class District { public int? DistrictId { get; set; } [Display(Name = "Employee District:")] public string DistrictName { get; set; } } } </code></pre> <p>EmployeeReferral Controller:</p> <pre><code>namespace Project.Controllers { public class EmployeeReferralController : Controller { private ProjDbContext db = new ProjDbContext(); // // GET: /EmployeeReferral/ public ActionResult Index() { return View(); } public ActionResult Create() { ViewBag.Districts = db.Districts; return View(); } </code></pre> <p>View:</p> <pre><code>&lt;td&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.DistrictId, "District") &lt;/div&gt; &lt;/td&gt; &lt;td&gt; &lt;div class="editor-field"&gt; @*@Html.DropDownList("DistrictId", "----Select ---")*@ @Html.DropDownListFor(model =&gt; model.DistrictId, new SelectList(ViewBag.Districts, "DistrictId", "DistrictName"), "--- Select ---") @Html.ValidationMessageFor(model =&gt; model.DistrictId) &lt;/div&gt; &lt;/td&gt; </code></pre> <p>Why can't we use ViewBag for populating dropdownlists that can be validated with Annotations. It is less lines of code.</p>
    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