0

I am using the following code with my MVC Html View

@Html.DropDownListFor(m => m.Company,
                new SelectList(ViewBag.Companies, "Key", "Value", Model.Company))

My controller makes the like this:

[HttpGet]
public ActionResult Login()
{
    ViewBag.Companies = (from a in _context.Companies 
                         select new {Key = a.Id, Value = a.Name});
    return View();
}

But shows the following error:

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 30: Line 31: Line 32:
@Html.DropDownListFor(m => m.Company, Line 33: new SelectList(ViewBag.Companies, "Key", "Value", Model.Company)) Line 34:

This code is used to so a login screen where its Username, Password and Dropdown for Company.

3
  • 1
    What is the @model of your view? In your Controller action you need to pass in an instance to the View() so something like : return View(new YourModel());... Commented Jan 12, 2014 at 20:48
  • but it works fine for e.g showing for example text boxes based on the model? Just not dropdowns? Commented Jan 12, 2014 at 20:49
  • It is not working because you are accessing the Model.Company in your helper and your Model is null. Just write @Html.DropDownListFor(m => m.Company, new SelectList(ViewBag.Companies, "Key", "Value")) Commented Jan 12, 2014 at 20:56

2 Answers 2

1

You are referencing Model.Company in your view. That would be null since it is never passed from the controller.

Sign up to request clarification or add additional context in comments.

3 Comments

I understand that but its for a create scenrio so there is no value for it. <div class="editor-label"> @Html.LabelFor(model => model.Password) </div> <div class="editor-field"> @Html.EditorFor(model => model.Password) @Html.ValidationMessageFor(model => model.Password) </div>
The above code works fine. So i dont understnad why a dropdown doent
The difference is Html."Control"For probably interpreters that the model is null and display the control anyway. Where new SelectList(ViewBag.Companies, "Key", "Value", Model.Company) does not scrutinize to that level and throws the exception on Model.Company. Model is null!
1

First you should set your ViewModel in your View like this:

@model YourClassname

Second, you should get your Company and when you returning your View you should pass your item like this:

[HttpGet]
public ActionResult Login()
{
    ViewBag.Companies = (from a in _context.Companies 
                     select new {Key = a.Id, Value = a.Name});
    // get your Company 
    var cmp = _context.Companies.First(); //for example

    return View(cmp);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.