0

In my ViewModel, I'm passing an IEnumerable property loaded with a list of values for a drop-down box on the web page:

// Drop-down list values...
public IEnumerable< PAYMENT_METHODS > PaymentMethods { get; set; }

And I'm consuming them on the MVC view as follows:

@Html.DropDownListFor(x => Model.objConsultant.Profile.CDE_PAYMENT_METHOD, new SelectList(Model.PaymentMethods, "CDE_PAYMENT_METHOD", "TXT_PAYMENT_METHOD_DESCRIPTION"), htmlAttributes: new { id = "ddlbPaymentMethod" })

I have several buttons on the web page that perform post-back actions as the user works with the web page. The problem is that after the post-back, the PaymentMethods property is null, and thus throws an exception when the page loads. In order to work around this, I had to save the PaymentMethods data on a Session variable during the original GET Index action, and then restore the PaymentMethods property from the Session upon the POST Index action. Am I missing something? Why isn't it saving the IEnumerable data across post actions?

Steven
  • 13
  • 5
  • Of course it will be `null`. You are not (and definitely should not be) creating an input for each property of each item in your `PaymentMethods` collection so the client does not send anything related to it back to the server when you submit (suggest you refer [this answer](https://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o)) –  Dec 10 '17 at 23:31
  • Okay then, it sounds like my method of using the session for this list of drop-down values is reasonable then. I noticed that the sample that you linked they were using database fetch for each postback but I think in my case since the list of drop-down values is short and the application does not to scale terribly high and number of users that my method is okay. Thank you for confirming that I innumerable lists will not be safe to cross post backs. – Steven Dec 11 '17 at 00:12
  • And what happens if `Session` is recycled. Get you collection for the database again if you need it. And the only time you need it is is `ModelState` is invalid and you need to return the view which, assuming you have correctly implemented client side validation, will be hardly ever. –  Dec 11 '17 at 00:41

0 Answers0