0

So I basically have the same issue as this post: ASP.NET MVC - How to populate dropdownlist with another model?

The solution works... technically.. as long as the model is populated. However, I'm working with a CREATE view where the controller's action simply returns a blank "View()" obviously because it's creating something so you don't have any data yet.... yet I obviously still want the dropdownlist to populate from a model which gets it's data from the DB. If I try to run this code on a view with an empty model I get "Object reference not set to object" when it tries to grab the property that returns the list.

I tried instantiating a new, blank model in the Create action with the USING statement and set only PropertyTypeList property with a new instance of the Type model, and passed it to the view and it sort of worked... the view showed up with the dropdown of the other Type model populated, but it pre-filled in a bunch of the int/date types with 0's and 1/1/1900's because I have non-nullable fields. This is the closest I've gotten so far to simply letting the user create a new record with a pre-populated dropdown from ome of the fields that comes from a model.

I could just create a new Type model in the Create Action and assign it to the Viewbag or Tempdata, which I've done in the past, but this idea just makes me feel DIRTY. Viewbag disappears if the person refreshes the page so they get an error and is totally unprofessional. And I don't use Tempdata much because it relies on session state which gets very problematic if a user has my form open in mulitple tabs which could easily happen.

I feel like the solution from this post is SO close. It works fine when you're working with the EDIT action because you're passing a full model. But does anyone know how to get a dropdownlist to populate like this with an empty model? I tried something like adding an extra class to my secondary Type model

namespace blablanamespace {

    public partial class PropertyType {
    .. bla bla bla propertytype ID and name here
    }

    public class ViewModel
    {
        private readonly List<PropertyType> _keytypes;
        public IEnumerable<SelectListItem> PropTypeItems
        {
            get { return new SelectList(_keytypes, "TypeID", "TypeID"); }
        }
    }
}

and

@Html.DropDownListFor(model => model.TypeID, new SelectList(new blablanamespace.Models.ViewModel().PropTypeItems))

but then I just get this error:

Value cannot be null. Parameter name: items

If I change the ViewModel class to instantiate a new list of types like so

 public class ViewModel
    {
    public class ViewModel
    {
        private readonly List<PropertyType> _keytypes = new List<PropertyType>;
        public IEnumerable<SelectListItem> PropTypeItems
        {
            get { return new SelectList(_keytypes, "TypeID", "TypeID"); }
        }
    }

I don't get the error this time, but I just get a blank form(yay) with a blank dropdownlist(boo!!). I figured this latter method would work since when I want to populate a new fresh list in the controller I basically do the same thing

List<ApplicationKeyType> _keytypes = new List<ApplicationKeyType>();

That behavior doesn't appear to be the same in the model.

13
  • It makes no different if its a Create method or Edit method - you still pass an instance of your view model to the view. And as a side note, DropDownListFor requires IEnumerable<SelectListItem> so using new SelectList() in the view to create an another identical IEnumerable<SelectListItem> is pointless Commented Jan 31, 2018 at 21:09
  • Wel.. same thing if I just do this: @Html.DropDownListFor(model => model.TypeID, new blablanamespace.Models.ViewModel().KeyTypeItems) . Instead of creating a selectlist. Still.. a blank dropdownlist. There's got to be a simple way to have a create view with one empty model display a dropdownlist with a list of rows from another model. Commented Jan 31, 2018 at 21:22
  • 1
    Your property should be just public IEnumerable<SelectListItem> PropTypeItems { get; set; } and then your initialize a new instance of your view model, populate PropTypeItems and pass it to the view. Refer this Q/A for a typical example Commented Jan 31, 2018 at 21:23
  • The problem is I did initialize a new instance of my main view model, giving it this PropTypeItems property and passed it to my Create View. It showed the populated list of select items just fine, but all the other model's fields that were non-nullable were pre-set to either "0" or "1/1/1900" depending on whether or not it was an int or date. it's looks pretty unprofessional to present someone with a form to enter their data and to have this kind of stuff already filled in. Commented Feb 1, 2018 at 14:24
  • 1
    And its a view model so all the value type properties should be nullable anyway - refer this answer Commented Feb 1, 2018 at 21:11

0

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.