Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had same problem, and finally I got the answer...</p> <p>The problem is that in the POST action, after submitting the form, the ModelState is not valid, or it's catching an error in try/catch, so the View is returned. But this time the View has not the <code>ViewData["basetype"]</code> correctly set.</p> <p>You need to populate it again, probably with the same code used before, so repeat this:</p> <pre><code>var db = new DB(); IEnumerable&lt;SelectListItem&gt; basetypes = db.Basetypes.Select( b =&gt; new SelectListItem { Value = b.basetype, Text = b.basetype }); ViewData["basetype"] = basetypes; </code></pre> <p>before the <code>return View(meal)</code> in the <code>[HttpPost]</code> method.</p> <p>exactly this will solve your problem:</p> <pre><code>[HttpPost] public ActionResult Create(Meal meal) { if (ModelState.IsValid) { try { // TODO: Add insert logic here var db = new DB(); db.Meals.InsertOnSubmit(meal); db.SubmitChanges(); return RedirectToAction("Index"); } catch { var db = new DB(); IEnumerable&lt;SelectListItem&gt; basetypes = db.Basetypes.Select( b =&gt; new SelectListItem { Value = b.basetype, Text = b.basetype }); ViewData["basetype"] = basetypes; return View(meal); } } else { var db = new DB(); IEnumerable&lt;SelectListItem&gt; basetypes = db.Basetypes.Select( b =&gt; new SelectListItem { Value = b.basetype, Text = b.basetype }); ViewData["basetype"] = basetypes; return View(meal); } } </code></pre> <p>I know this question is very old, but I came here today with the same problem, so other could come here later...</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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