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.

citronsmurf
  • 90
  • 11
  • 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` so using `new SelectList()` in the view to create an another identical `IEnumerable` is pointless –  Jan 31 '18 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. – citronsmurf Jan 31 '18 at 21:22
  • 1
    Your property should be just `public IEnumerable 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](https://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) for a typical example –  Jan 31 '18 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. – citronsmurf Feb 01 '18 at 14:24
  • sorry.. I mean dates were "1/1/0001 12:00AM".... hence why it *does* matter if it's a create or edit view. If it's "EDIT", this doesnt matter because I don't have to worry about this problem. if it's create... the new model doesn't have any values yet so instantiating a new instance of one forces the non-nullable fields to have values I don't want. – citronsmurf Feb 01 '18 at 14:31
  • After digging around, it seems even MS suggests using the Viewbag Approach: https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions/working-with-the-dropdownlist-box-and-jquery/using-the-dropdownlist-helper-with-aspnet-mvc ... Using Viewbag just feels like a hack, though – citronsmurf Feb 01 '18 at 14:53
  • MS does not recommend `ViewBag` and neither does anyone who is proficient in mvc –  Feb 01 '18 at 21:05
  • 1
    And its a view model so all the value type properties should be nullable anyway - refer [this answer](https://stackoverflow.com/questions/43688968/what-does-it-mean-for-a-property-to-be-required-and-nullable/43689575#43689575) –  Feb 01 '18 at 21:11
  • And a non-nullable value type will display its default value in the view even if you do not pass a model to the view so your previous comments make no sense anyway. –  Feb 01 '18 at 21:17
  • Thanks for the link. I'll try making it nullable BUT add the Required attribute. If MS doesnt recommend using Viewbag, the sure seem to have a lot of documents such as the one I linked to where they suggest using it for dropdownlists. (Not that I like that idea) – citronsmurf Feb 02 '18 at 14:46
  • Also... You said "non-nullable" values will displaydefault value in the view even if you dont pass a model so "what I said makes no sense". Well, I'm just telling you what I'm seeing. I've done a basic "return view()" from the [httpget] Create action, not passing a model, and the textboxes *never* display a default value for their respective fields. They're always blank. You say it doesn't make sense, but that's what happening. Once I instantiated a new view without setting the properties and then passed it, 0's were showing up for non-nullable ints despite not having default values – citronsmurf Feb 02 '18 at 14:49
  • The only fields I have with default values are the dates, and even then when I passed a blank model, it didn't even populate with my default values (which were "getdate()"). They populated with "1/1/0001". – citronsmurf Feb 02 '18 at 14:49
  • FYI, the latest link where you suggest making all properties nullable, but adding the REQUIRED attribute did the trick Thanks you!. I'm creating my Models using "database first" so I'm not the one setting this property. Whether or not the model shows them as Nullable .. is coming automatically from EF getting it from my table/view/whatever object. If I add "nullable" to my required fields (who DONT have default values) but then slap a [Required] attribute just above the property and instantiate a new "blank' model and pass it, it stops these pre-filled values while still triggering validation – citronsmurf Feb 02 '18 at 15:55

0 Answers0