2

In my mvc4 application, I have drop down list set up on a page Register like this

In AccountModel.cs's HttpGet Register method, I initialize its data with this code

public ActionResult Register()
{
   MyModel mm=new MyModel();
   mm.mydropdown=new []
   { 
      new SelectListItem{Value="value", Text="text"}
      ////...
   };
}

Only when this page loads, will that code get run; but is there anywhere I can insert the code snippet that it must be compiled and have my mydropdown always initialized in the application such that I don't have to rewrite it in [HttpPost] method ?

[update] Here is the mymodel class

public class Register
{
   [Required(ErrorMessage="Option is required")]
   public string option;

   public IEnumerable<SelectListItem> Options;
}

In my case I use option in cshtml like this

@Html.DropDownListFor(m=>m.option, Model.Options,"Select an item")

In HttpPost Register method, I must reinitialize the Options or I will run into error "Option is of type string but it must be IEnumerable<SelectListItem> instead" otherwise

Asp Asp
  • 864
  • 1
  • 7
  • 15
  • 1
    I favor helper classes (I call them ["composers"](http://stackoverflow.com/a/14132184/453277)) for setting up my models. That way, I can make a single call and prepare the model from any number of methods. It becomes extremely useful when you have many properties to repopulate. To answer your original question, there isn't any automatic state management (as there was in web forms) to make the dropdown "just work". – Tim M. Feb 27 '13 at 01:16
  • Thank you Tim very much :D, I will try to keep up with what you suggest soon. – Asp Asp Feb 27 '13 at 01:53
  • No problem. Yours is a very good question...too often controllers become unmanageable because they start "doing too much" to the view models they are preparing. – Tim M. Feb 27 '13 at 02:15

1 Answers1

1

You could extend IEnumerable<SelectListItem> and do it like this

public class MyDropDownList : IEnumerable<SelectListItem> 
{
    public MyDropDownList()
    { 
          SelectedListItem item = new SelectListItem{Value="value", Text="text"};
          this.Add(item);
    }
}

Alternatively, you could write it into the constructor of MyModel

public class Register
{
    [Required(ErrorMessage="Option is required")]
    public string option;

    public IEnumerable<SelectListItem> Options;
    public Register()
    {
         this.Options = new []
         { 
            new SelectListItem{Value="value", Text="text"}
            ////...
         };
    }
}
public ActionResult Register()
{
   Register mm = new Register();
}
corylulu
  • 3,449
  • 1
  • 19
  • 35
  • thank you, in my OP, `mydropdown` object is declared as `public IEmumerable mydropdown`. If I use your method, then how can I redeclare the variable and use it in `cshtml` page ? – Asp Asp Feb 27 '13 at 01:24
  • I slightly modified my answer based on what you said, but why not do this during the initialization of the `MyModel` class? Can you also post the `MyModel` class to better see what available options there are? – corylulu Feb 27 '13 at 01:31
  • Okay, then look at my second example. Create a constructor in your `Register` class like what I showed. – corylulu Feb 27 '13 at 02:01
  • Thank you, I could eliminate the redundant code in the `HttpPost` now using your suggestion of the constructor method. :) – Asp Asp Feb 27 '13 at 02:09