ASP.NET MVC Form repopulation

Last post 01-30-2009 9:16 PM by jdc. 2 replies.

Sort Posts:

  • ASP.NET MVC Form repopulation

    01-28-2009, 5:10 PM
    • Member
      510 point Member
    • aspBOD
    • Member since 01-21-2005, 11:11 AM
    • Posts 118

    I have a controller with two actions:

    [AcceptVerbs("GET")]
       
    public ActionResult Add()
       
    {
           
    PrepareViewDataForAddAction();
           
    return View();
       
    }


    [AcceptVerbs("POST")]
       
    public ActionResult Add([GigBinderAttribute]Gig gig, FormCollection formCollection)
       
    {
           
    if (ViewData.ModelState.IsValid)
           
    {
               
    GigManager.Save(gig);
               
    return RedirectToAction("Index", gig.ID);
           
    }
           
    PrepareViewDataForAddAction();
           
    return View(gig);
       
    }

    As you can see, when the form posts its data, the Add action uses a GigBinder (An implemenation of IModelBinder)

    In this binder I have:

     if (int.TryParse(bindingContext.HttpContext.Request.Form["StartDate.Hour"], out hour))
           
    {
               gig
    .StartDate.Hour = hour;
           
    }
           
    else
           
    {
                bindingContext
    .ModelState.AddModelError("Doors", "You need to tell us when the doors open");
           
    }

    The form contains a text box with id "StartDate.Hour".

    As you can see above, the GigBinder tests to see that the user has typed in an integer into the textbox with id "StartDate.Hour". If not, a model error is added to the modelstate using AddModelError.

    Since the gigs property gigs.StartDate.Hour is strongly typed, I cannot set its value to, for example, "TEST" if the user has typed this into the forms textbox.

    Hence, I cant set the value of gigs.StartDate.Hour since the user has entered a string rather than an integer.

    Since the Add Action returns the view and passes the model (return View(gig);) if the modelstate is invalid, when the form is re-displayed with validation mssages, the value "TEST" is not displayed in the textbox. Instead, it will be the default value of gig.StartDate.Hour.

    How do I get round this problem? I really stuck!

    Filed under:
  • Re: ASP.NET MVC Form repopulation

    01-29-2009, 11:03 AM
    • Contributor
      6,325 point Contributor
    • Augi
    • Member since 10-18-2008, 9:46 AM
    • Czech Republic
    • Posts 1,096

    I'm afraid that it cannot be easily done Sad

    Don't forget to click "Mark as Answer" on the post that helped you.
  • Re: ASP.NET MVC Form repopulation

    01-30-2009, 9:16 PM
    Answer
    • Member
      17 point Member
    • jdc
    • Member since 08-21-2008, 9:22 PM
    • Posts 9

    Somewhere within your ModelBinder, there's a property you can set - I can't remember exactly, but I think it's something like "AttemptedValue" - and if you set that, it'll show up when the page is redisplayed.  Grab Reflector and look into how the DefaultModelBinder is implemented, I think that's where I found it the first time.

     

Page 1 of 1 (3 items)
Microsoft Communities