fivunlm
fivunlm

Reputation: 462

DropDownListFor Object reference not set to an instance of an object

Im using a ModelView in order to create a DropDownList:

ModelView

public class CreateSubjectModel
{
    public Subject Subject { get; set; }
    public IEnumerable<SelectListItem> Careers { get; set; }
}

The Action [HttpGet]

 public ActionResult Create()
    {
        CreateSubjectModel model = new CreateSubjectModel();

        List<SelectListItem> careerList = new List<SelectListItem>();
        foreach (var career in _careerManager.GetCareers())
            careerList.Add(new SelectListItem() { Text = career.Institution + " - " +     career.Name, Value = career.id.ToString() });    

        model.Careers = careerList;

        return View(model);
    } 

The Action [HttpPost]

public ActionResult Create(CreateSubjectModel createSubjectModel)
        {
            try
            {
                _subjectManager.SaveSubject(createSubjectModel.Subject);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

And the View:

  <div class="editor-field">
        @Html.DropDownListFor(model => model.Careers, Model.Careers)
        @Html.ValidationMessageFor(model => model.Careers)
    </div>

The problem is that when I hit Submit I have this problem:

**Object reference not set to an instance of an object.**
Description: An unhandled exception occurred during the execution of the current web   request. Please review the stack trace for more information about the error and where it  originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 47:         </div>
Line 48:         <div class="editor-field">
Line 49:             @Html.DropDownListFor(model => model.Careers, Model.Careers)
Line 50:             @Html.ValidationMessageFor(model => model.Careers)
Line 51:         </div>

I look everywhere and i cant find a working example or some tutorial that helps me. Hope you can do...

Upvotes: 2

Views: 5420

Answers (2)

John Landheer
John Landheer

Reputation: 4019

Two things:

First, you are not creating controls that will populate your Subject model, so that is null in your Create action. You haven't posted that model, so I can't tell you what the razor syntax should be.

Second: You are binding creating a dropdown list for the list itself:

@Html.DropDownListFor(model => model.Careers, Model.Careers)

Change you model to:

public class CreateSubjectModel
{
    public Subject Subject { get; set; }
    public IEnumerable<SelectListItem> Careers { get; set; }
    public int SelectedCareerId { get; set; }
}

and your view to:

@Html.DropDownListFor(model => model.SelectedCareerId, Model.Careers)

This new property will have the value selected in your Create action.

Upvotes: 0

Samich
Samich

Reputation: 30125

Probably you have an Exception in the your code in in the catch block you returnign View without model. And in this case on the view you have exception, because of model is null.

try
{
    _subjectManager.SaveSubject(createSubjectModel.Subject);
    return RedirectToAction("Index");
}
catch
{
    return View(); // problem here.. return View with model too.
}

Upvotes: 3

Related Questions