Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble with ASP.NET MVC 3 DropDownListFor
    text
    copied!<p>I've been researching this issue, but haven't been able to piece together an answer that works in my situation. I appreciate any help.</p> <p>I'm trying to create a drop-down list that will show Application names in a Create Task page, but posts the Application ID back to the database. Currently, I'm able to get the Application names to show in the drop-down list correctly when I use DropDownList. However, the post back doesn't work, and I'd like to use DropDownListFor instead. Also, I'm using Dictionary(int, String>, but I'd prefer to use IEnumerable. I've been going around in circles trying to get the DropDownListFor to work, but no luck so far. Does anyone know what changes I need to make?</p> <p>'Task' Model:</p> <pre><code>using System; using System.Collections.Generic; using System.Web.Mvc; namespace TaskCapture.Models { public class Task { public int TaskID { get; set; } public string TaskDescription { get; set; } public int ApplicationID { get; set; } public virtual Application Application { get; set; } } } </code></pre> <p>'Application' Model:</p> <pre><code>using System; using System.Collections.Generic; namespace TaskCapture.Models { public class Application { public int ApplicationID { get; set; } public string ApplicationName { get; set; } public override string ToString() { return ApplicationName; } } } </code></pre> <p>TaskController:</p> <pre><code>using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Web; using System.Web.Mvc; using TaskCapture.Models; using TaskCapture.DAL; namespace TaskCapture.Controllers { public class TaskController : Controller { private SupportContext db = new SupportContext(); // // GET: /Task/Create // get application names for listing in drop down public Dictionary&lt;int, String&gt; GetApplications() { Dictionary&lt;int, String&gt; _applications = new Dictionary&lt;int, String&gt;(); var applicationsQuery = from o in db.Applications orderby o.ApplicationName select o; foreach (var c in applicationsQuery) { _applications.Add(c.ApplicationID, c.ApplicationName); } return _applications; } public ActionResult Create() { Dictionary&lt;int, String&gt; Applications = this.GetApplications(); ViewData["ApplicationList"] = new SelectList(Applications, "Key", "Value"); return View(); } // // POST: /Task/Create [HttpPost] public ActionResult Create(Task task) { try { if (ModelState.IsValid) { db.Tasks.Add(task); db.SaveChanges(); return RedirectToAction("Index"); } } catch (DataException) { //Log the error (add a variable name after DataException) ModelState.AddModelError("", "Unable to save changes. Try again...."); } return View(task); } } } </code></pre> <p>Task Create.cshmtl:</p> <pre><code>@model TaskCapture.Models.Task @{ ViewBag.Title = "Create"; } &lt;h2&gt;Create&lt;/h2&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"&gt;&lt;/script&gt; @using (Html.BeginForm()) { @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;legend&gt;Task&lt;/legend&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Application.ApplicationName) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.DropDownList("ApplicationList", (SelectList) ViewData["ApplicationList"]) @Html.ValidationMessageFor(model =&gt; model.Application.ApplicationName) &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Create" /&gt; &lt;/p&gt; &lt;/fieldset&gt; } &lt;div&gt; @Html.ActionLink("Back to List", "Index") &lt;/div&gt; </code></pre> <p>Any help is appreciated. Please let me know, if I need to post more info. Thanks.</p>
 

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