Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET EditorTemplate DropdownList
    text
    copied!<p><strong>Every time I add a new App It creates a new AppCategory</strong>. I am seriously screwing this up somehow </p> <p>code first entity framework objects</p> <pre><code>public class AppCategory { public int ID { get; set; } public string Name { get; set; } public ICollection&lt;App&gt; apps { get; set; } } public class App { public int ID { get; set; } public string Name { get; set; } public AppCategory Category { get; set; } } </code></pre> <p>Editor Template (I would love to just make just one Foreign Key EditorTemplate)</p> <pre><code>@inherits System.Web.Mvc.WebViewPage @Html.DropDownList("Category", LIG2010RedesignMVC3.Models.Repo.GetAppCategoriesSelect()) </code></pre> <p>and of course the repository</p> <pre><code> public static IEnumerable&lt;SelectListItem&gt; GetAppCategoriesSelect() { return (from p in GetAppCategories() select new SelectListItem { Text = p.Name, Value = p.ID.ToString(), }); } public static ICollection&lt;AppCategory&gt; GetAppCategories() { var context = new LIGDataContext(); return context.AppCategories.ToList(); } </code></pre> <p>Every time I add a new App It creates a new AppCategory I am seriously screwing this up somehow</p> <p><hr> <strong>Adding more debug info</strong></p> <pre><code> @inherits System.Web.Mvc.WebViewPage @Html.DropDownList("", LIG2010RedesignMVC3.Models.Repo.GetAppCategoriesSelect()) </code></pre> <p>gives me a validation message on the post</p> <pre><code> Parameters application/x-www-form-urlencoded Category 1 Name 8 </code></pre> <p>Validation error <strong>The value '1' is invalid.</strong><br> This makes sense because Category should be an object not an integer.</p> <hr> <p><strong>Controller Code as asked for</strong> pretty sure this isnt the problem as it came from MVCScaffold</p> <pre><code> [HttpPost] public ActionResult Create(App d) { if (ModelState.IsValid) { context.Apps.Add(d); context.SaveChanges(); return RedirectToAction("Index"); } return View(); } </code></pre>
 

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