0

In my controller, I'm fetching some data to fill a combobox in the view. When data is posted, I check for

ModelState.IsValid

property, if it doesn't, I need to return to the view to show validations messages errors. However, the model only contains the posted data and the other needed to load the combobox is null and it throws a NullReferenceException. Which is the right way to solve this?

public ActionResult Index(){
        CourtBussines courtBussines = new CourtBussines();
        IList<Court> courts = new List<Court>();
        courts.AddRange(courtBussines.GetCourtsOpenedList());
        CourtSelectionModel courtSelectionModel = new CourtSelectionModel{Courts = courts, SelectedCourtId = -1};
        return View(courtSelectionModel);
    }

    [Authorize]
    [HttpPost]
    public ActionResult Index(CourtSelectionModel courtSelectionModel){
        if (!ModelState.IsValid){
            return View(courtSelectionModel); //Here, the data to load combobox is null and fails.
        }
        return RedirectToAction("Horarios", courtSelectionModel);
    }
2
  • You have to reload the data for the combobox every time you call the view! You should post your view and the exception as well! Commented Apr 3, 2014 at 20:08
  • 1
    @Oscar : On load event of View, combobox expects its items. So, you have to provide combo box items to view whenever you load the page. Commented Apr 3, 2014 at 20:11

1 Answer 1

2

You would have to re-initialize the Courts list, as the entire list is not posted.

Try something like.

[Authorize]
[HttpPost]
public ActionResult Index(CourtSelectionModel courtSelectionModel){
    if (!ModelState.IsValid){
        IList<Court> courts = new List<Court>();
        courts.AddRange(courtBussines.GetCourtsOpenedList());
        courtSelectionModel.Courts = Courts;
        courtSelectionModel.SelectedCourtId = -1;
        return View(courtSelectionModel); //Here, the data to load combobox is null and    fails.
    }
    return RedirectToAction("Horarios", courtSelectionModel);
}
Sign up to request clarification or add additional context in comments.

4 Comments

It' what I though, but it seems to me very rude to have to reinitialize all this data again, and asked to see if there is a better pattern to solve this.
Unfortunately, there isn't any better way that I know of, as the POST is only going to send back the value of the combo box, and not all the items that were used to populate the combo box.
Is a good idea to save the data in the cache to avoid databases hits?
I suppose you could, but MVC is largely stateless, so I would advise against it. I wouldn't really be concerned with database hits here, as you have to hit the database to load the view initially, so this in effect is just reloading the page as is, but with the extra model errors. Theoretically, someone could load the inital page, then just F5 repeatedly and hit your database every time.

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.