MVC3 Dropdown List returning NULL in Model Object

I am trying to get a dropdown list to send back it's selection on HTTP POST along with the other related data. I can see the value inside a Form Collection, but I VS2010 tells me that the object is NULL when I look at the other posted entities.

This model has a many to one relationship:

[Key]
public int ProgramTypeId { get; set; }

public string ProgramType { get; set; }

public List<SurveyProgramModels> SurveyProgramModel { get; set; }

This model uses the ProgramTypeId as a foreign key

public class SurveyProgramModels
    {
        [Key]
        public Guid ProgramId { get; set; }

        public virtual SurveyProgramTypeModels SurveyProgramTypeModels { get; set; }

        public int ProgramYear { get; set; }

        public int ProgramStatus { get; set; }

    }

Controller GET/POST

  //
        // GET: /SurveyProgram/Create

        public ActionResult Create()
        {

            SelectList typelist = new SelectList(db.SurveyProgramTypeModels.ToList(), "ProgramTypeId", "ProgramType", db.SurveyProgramTypeModels);
            ViewData["SurveyProgramTypeModels"] = typelist;

            return View();
        } 

        //
        // POST: /SurveyProgram/Create

        [HttpPost]
        public ActionResult Create(SurveyProgramModels surveyprogram, FormCollection collection)
        {


            SelectList typelist = new SelectList(db.SurveyProgramTypeModels.ToList(), "ProgramTypeId", "ProgramType", db.SurveyProgramTypeModels);
            ViewData["SurveyProgramTypeModels"] = typelist;

          //  int SelectedCollection = Int32.Parse(collection["ProgramTypeId"]);

            if (ModelState.IsValid)
            {
              surveyprogram.ProgramId = Guid.NewGuid();

                db.SurveyPrograms.Add(surveyprogram);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(surveyprogram);
        }

Select list in the view:

<div class="editor-field">
            @Html.DropDownList("ProgramTypeTypeModels", (IEnumerable<SelectListItem>)ViewData["SurveyProgramTypeModels"])
            @Html.ValidationMessageFor(model => model.SurveyProgramTypeModels)
        </div>

The Select list is correct when rendered in the view, and I can see the selected value as a part of the form collection. I'm not sure why the selected value isn't being saved along with the other data for SurveyProgramModels.

Edit: I should also mention the data submits correctly when I remove the relationship between the two classes.

Answers


The form element is named as "ProgramTypeTypeModels" which would cause it not to bind back correctly. You also should create a Model to return to the View.

Then include the SelectList as a property in the Model for the View

Now you do something like:

 @Html.DropDownListFor(model => model.SurveyProgramTypeModels)

And for the [HttpPost] you can then take the strongly typed Model as an argument and the aspnet binding will do all the work, no reason to DTO out of the FormCollection


Need Your Help

Find the name and size of the uploaded document using Fiddler?

sharepoint moss sharepoint-2007 fiddler

Will I be able to identify the name and size of a document that I am trying to upload into a SharePoint 2007 document library using Fiddler2 tool?

Can one main thread call other main thread in java

java multithreading

I am looking for some more knowledge about java main method public static void main(String[] args) .When JVM call main method it creates Main thread and the whole program is get executed in this Main