2

I am populating DropDownList from in-memory data and getting this error on POST.

Error: The ViewData item that has the key 'MaritalStatus' is of type 'System.String' but must be of type 'IEnumerable'.

Controller:-

// GET: /Application/Create
public ActionResult Create()
{
    List<SelectListItem> lst = new List<SelectListItem>();
    lst.Add(new SelectListItem { Text = "Unmarried", Value = "1" });
    lst.Add(new SelectListItem { Text = "Married", Value = "2" });
    lst.Add(new SelectListItem { Text = "Widow", Value = "3" });
    ViewBag.MaritalStatus = new SelectList(lst, "Value", "Text");

    return View();
}

// POST: /Application/Create
[HttpPost]
public ActionResult Create(ApplicationForm applicationform)
{
    if (ModelState.IsValid)
    {
        db.ApplicationForms.Add(applicationform);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(applicationform);
}

View:-

<div class="editor-label">
    @Html.LabelFor(model => model.MaritalStatus)
</div>
<div class="editor-field">
    @Html.DropDownList("MaritalStatus")
    @Html.ValidationMessageFor(model => model.MaritalStatus)
</div>

Model Property:-

[DisplayName("Marital Status")]
public string MaritalStatus { get; set; }

Any help is appreciated.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
Abhimanyu
  • 2,173
  • 2
  • 28
  • 44
  • @stephen how do you say this is duplicate ? this question was asked 3 years ago, and the question you referenced to be original was asked 6 months ago. – Abhimanyu Jun 20 '16 at 12:42
  • That makes no difference. It gives users coming across this question an alternative answer (which is a community wiki and explains in depth what the issue is) –  Jun 20 '16 at 12:44
  • Whole point of the marking a question duplicate is to tell everybody to ignore it so they can make better use of their time. This is what an expert explained here http://meta.stackoverflow.com/questions/260607/how-to-remove-duplicate-mark-from-my-question. So, technically, you should have marked newer question as duplicate. – Abhimanyu Jun 20 '16 at 12:47
  • You said "It gives users coming across this question an alternative answer (which is a community wiki and explains in depth what the issue is)" I see a RELATED question list in the right side, i personally use this as an alternative. If I see duplicate I do not value that question. – Abhimanyu Jun 20 '16 at 12:50
  • What? There are hundreds of great questions on this site that are marked as duplicates. Having your question marked as duplicate does not mean it a poor question at all. –  Jun 20 '16 at 12:52
  • I just wanted to understand use of marking question duplicate and use of related question list in the right side. Can we mark any question as duplicate without looking at timestamp ? Let me raise a query to support. Thanks. – Abhimanyu Jun 20 '16 at 13:02

1 Answers1

4

Well, i solved this by repopulating the List in POST action, here is the complete working code.

    //
    // GET: /Application/Create

    public ActionResult Create()
    {
        List<SelectListItem> lst = new List<SelectListItem>();
        lst.Add(new SelectListItem { Text = "Unmarried", Value = "Unmarried" });
        lst.Add(new SelectListItem { Text = "Married", Value = "Married" });
        lst.Add(new SelectListItem { Text = "Widow", Value = "Widow" });
        ViewBag.MaritalStatus = new SelectList(lst, "Value", "Text");

        return View();
    }

    //
    // POST: /Application/Create

    [HttpPost]
    public ActionResult Create(ApplicationForm applicationform)
    {
        if (ModelState.IsValid)
        {
            db.ApplicationForms.Add(applicationform);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        else
        {
            List<SelectListItem> lst = new List<SelectListItem>();
            lst.Add(new SelectListItem { Text = "Unmarried", Value = "1" });
            lst.Add(new SelectListItem { Text = "Married", Value = "2" });
            lst.Add(new SelectListItem { Text = "Widow", Value = "3" });
            ViewBag.MaritalStatus = new SelectList(lst, "Value", "Text");
        }

        return View(applicationform);
    }
Abhimanyu
  • 2,173
  • 2
  • 28
  • 44