Object reference not set to an instance of an object. ModelState.SetModelValue - BUG ??

Last post 07-05-2009 1:30 AM by zielony. 8 replies.

Sort Posts:

  • Object reference not set to an instance of an object. ModelState.SetModelValue - BUG ??

    07-03-2009, 2:30 AM
    • Member
      46 point Member
    • zielony
    • Member since 06-24-2009, 2:22 AM
    • Posts 164

    I have in controller:

    [AcceptVerbs(HttpVerbs.Post)]
            public ActionResult Create([Bind(Exclude = "id_book")] books bok)
            {
                Books b = new Books();
                ViewData["id_author"] = new MultiSelectList(b.TakeAuthors(), "idAuthor", "author");
                
               
                if (Request.Form["id_author"] == null || Request.Form["id_author"].Trim().Length == 0)
                {
                    ModelState.AddModelError("id_author", "You must choose authors.");
                    ModelState.SetModelValue("id_author", ValueProvider["id_author"]);                
                }
                if (!ModelState.IsValid)
                {
                    return View(bok);
                } 
    
    }


    In view:

    <%= Html.ListBox("id_author", null, new { size = 5 })%>
    <%= Html.ValidationMessage("id_author", "*") %>


    And when I don't choose author I have an error:

    Object reference not set to an instance of an object.

    I have found article about that:

    http://www.crankingoutcode.com/?aspxerrorpath=/2009/02/01/IssuesWithAddModelErrorSetModelValueWithMVCRC1.aspx

    But in my case ModelState.SetModelValue doesn't work so I think it can be bug ? Am I right ?

  • Re: Object reference not set to an instance of an object. ModelState.SetModelValue - BUG ??

    07-03-2009, 4:46 AM
    • Contributor
      4,231 point Contributor
    • levib
    • Member since 07-23-2007, 11:50 PM
    • Redmond, WA
    • Posts 749
    • AspNetTeam

    Are you sure ValueProvider["id_author"] is not null?

  • Re: Object reference not set to an instance of an object. ModelState.SetModelValue - BUG ??

    07-03-2009, 4:57 AM
    • Member
      46 point Member
    • zielony
    • Member since 06-24-2009, 2:22 AM
    • Posts 164

    hm...... What do you mean ? I don't understand ValueProvider so I copied and pasted inside him name of Html.ListBox: ValueProvide[" here_I_copied_name "]. Did I do it wrong ? :D So how should it look like ?

    PS. In Symfony framework + php + Doctrine wasn't so complicated things like ValueProvider :P

  • Re: Object reference not set to an instance of an object. ModelState.SetModelValue - BUG ??

    07-03-2009, 11:00 AM
    • Member
      46 point Member
    • zielony
    • Member since 06-24-2009, 2:22 AM
    • Posts 164

    No answers ? As I thought - nobody understand this STUPID ValueProvider :/ I don't know what I should do now. :(

  • Re: Object reference not set to an instance of an object. ModelState.SetModelValue - BUG ??

    07-03-2009, 2:27 PM
    • Contributor
      6,537 point Contributor
    • bitmask
    • Member since 07-29-2003, 3:18 PM
    • Citizen of the Earth
    • Posts 1,228

    The ValueProvider represents a collection of name/value pairs that are present somewhere in the request. It puts together all the name/value pairs from posted form values, the query string, and routing data (plus, adds some relavent culture information to each).

    If you don't have an id_author selected, then you won't have an id_author in the Request.Form collection, and it won't be in the ValueProvider either (unless you have it in the query string or in the routing data). Your code is passing a null value to SetModelValue, which is going to make other parts of the framework unhappy.

    The quickest hack would be to pass in a ValueProviderResult for id_author with empty strings, but in the long run I'd look at the model binding examples so you can encapsulate all this work.

    Scott
    http://www.OdeToCode.com/blogs/scott/
  • Re: Object reference not set to an instance of an object. ModelState.SetModelValue - BUG ??

    07-04-2009, 2:24 AM
    • Member
      46 point Member
    • zielony
    • Member since 06-24-2009, 2:22 AM
    • Posts 164

    As I understand second parameter is string with id selected authors for example: "1,5" yes ?

    What about first parameter ? I don't understand

  • Re: Object reference not set to an instance of an object. ModelState.SetModelValue - BUG ??

    07-04-2009, 9:34 AM
    • Member
      46 point Member
    • zielony
    • Member since 06-24-2009, 2:22 AM
    • Posts 164

    PEOPLE I HAVE A SOLUTION for mine ListBox!!!!!!!!!!!!

    I had to add:

    ModelState.SetModelValue("id_author", new ValueProviderResult(null, "", null));


    ViewData["id_author"] = new MultiSelectList(b.TakeAuthors(), "idAuthor", "author", Request.Form["id_author"]);

    if (Request.Form["id_author"] == null)
                {
                    ModelState.AddModelError("id_author", "You must choose authors.");
                    ModelState.SetModelValue("id_author", new ValueProviderResult(null, "", null));
                }   




    I don't know who and why has "invented" that but I think THIS IS VERY STUPID !!

  • Re: Object reference not set to an instance of an object. ModelState.SetModelValue - BUG ??

    07-04-2009, 4:20 PM
    Answer
    • Contributor
      4,231 point Contributor
    • levib
    • Member since 07-23-2007, 11:50 PM
    • Redmond, WA
    • Posts 749
    • AspNetTeam

    zielony:
    I don't know who and why has "invented" that but I think THIS IS VERY STUPID !!

    See this post for the context around these two methods.  Also, if you would like the help of the product team and others in these forums, it wouldn't hurt to be a bit more professional in your demeanor. :)

  • Re: Object reference not set to an instance of an object. ModelState.SetModelValue - BUG ??

    07-05-2009, 1:30 AM
    • Member
      46 point Member
    • zielony
    • Member since 06-24-2009, 2:22 AM
    • Posts 164

    English isn't my national language so as I understand - SetModelValue is used to return old values when form had errors. But I have:

    ViewData["id_author"] = new MultiSelectList(b.TakeAuthors(), "idAuthor""author"Request.Form["id_author"]);

    thx 4. argument in MultiSelectList I return to ListBox old selected values so I still don't understand ModelState.SetModelValue in that case - I must have ModelState.SetModelValue but it does nothing.

Page 1 of 1 (9 items)