razor - Unable to bind a POST request from a form containing a dropdownlist -


i getting requestbindingexception when post form contains dropdownlist. additionally, server returns http 400 bad request following post data:

address1:address1 address2:address2 city:city county:county countries:gb postalcode:test123 

i'm unsure i'm doing wrong. trying assign countries / selectedcountry properties in viewmodel's constructor? if so, how should assigning initial / default values these properties?

or bad idea use viewmodel endpoint parameter (should separate dto?)

my viewmodel looks this:

public class addressdetailsviewmodel {     public addressdetailsviewmodel()     {         countries = new list<selectlistitem>                         {                             new selectlistitem                                 {                                     selected = true,                                     text = "united kingdom",                                     value = "gb"                                 }                         };          selectedcountry = new list<selectlistitem>                         {                             new selectlistitem                                 {                                     selected = true,                                     text = "united kingdom",                                     value = "gb"                                 }                         };     }     ... } 

and i'm using html helper create dropdown list:

        <li>             @html.labelfor(x => x.countries)             @html.dropdownlistfor(x => x.countries, model.selectedcountry)         </li> 

my service endpoint (won't hit) looks like:

public object post(addressdetailsviewmodel data) {     ... } 

the short answer recommend not using viewmodel endpoint parameter, , instead design separate dto represent message want send server. if create clean message dto represents exact fields want in request, simple get/set properties each field in request, should easier wire , routing work in servicestack.

there few pitfalls reusing view model request message:

  • an mvc style viewmodel great describing display on web page, it's not representation of server cares about
  • the view model meant single purpose, simple representation of specific type of view wants display
  • a dto representing request message, on other hand, can , should more abstract, representing operation want perform on server (or identity , state of resource if want restful)
  • your service can , maybe should able accept given type of message variety of different clients. if in future want sort of action on server using ajax request instead of form post? or if want mobile app or third party client integrate service? separate message dto class describes intent of action on server rather reflecting state of ui here

more specifically, actual reason you're getting requestbindingexception due couple of factors, how countries property/field declared in addressdetailsviewmodel class, how routing defined request compared how html form declared, etc. initializing data in constructor of view model shouldn't relevant 400 error seeing; i've found that's way initialize default values dtos. hard tell without seeing more code.

i'd recommend designing simpler dto class post request has properties necessary data server needs. , use simpler datatypes - strings , ints instead of selectlistitems or lists of selectlistitems. complicate request , possibly involved in 400 error seeing.


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -