c# - Why is the model binder mixing up ID fields? -
i have model used add new address, in view shopid
in hiddenfor
, id
0
, fk shop
null
there no id
set in it.
public int id { get; set; } public string shopid { get; set; } public string addressline1 { get; set; } public string addressline2 { get; set; } public string addressline3 { get; set; } public string district { get; set; } public string region { get; set; } public string postalcode { get; set; } public int countryid { get; set; } public system.datetime creationtimestamp { get; set; } public bool isprimary { get; set; } public nullable<double> latitude { get; set; } public nullable<double> longitude { get; set; } public virtual country country { get; set; } public virtual shop shop{ get; set; }
when post form modelstate.isvalid
showing false
saying value in shopid
invalid in id
field:
the value 'd04bf59f-be29-4896-ae70-54432b02aa46' not valid id.
why model binding placing shopid
id
field?
here's view:
@using (html.beginform()) { @html.antiforgerytoken() <div class="form-horizontal"> <h4>shopaddress</h4> <hr /> @html.validationsummary(true) @html.hiddenfor(m => model.shopid) <div class="form-group"> @html.labelfor(model => model.addressline1, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.textboxfor(model => model.addressline1, new { @class = "form-control" }) @html.validationmessagefor(model => model.addressline1) </div> </div> <div class="form-group"> @html.labelfor(model => model.addressline2, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.textboxfor(model => model.addressline2, new { @class = "form-control" }) @html.validationmessagefor(model => model.addressline2) </div> </div> <div class="form-group"> @html.labelfor(model => model.addressline3, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.textboxfor(model => model.addressline3, new { @class = "form-control" }) @html.validationmessagefor(model => model.addressline3) </div> </div> <div class="form-group"> @html.labelfor(model => model.district, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.textboxfor(model => model.district, new { @class = "form-control" }) @html.validationmessagefor(model => model.district) </div> </div> <div class="form-group"> @html.labelfor(model => model.region, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.textboxfor(model => model.region, new { @class = "form-control" }) @html.validationmessagefor(model => model.region) </div> </div> <div class="form-group"> @html.labelfor(model => model.postalcode, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.textboxfor(model => model.postalcode, new { @class = "form-control" }) @html.validationmessagefor(model => model.postalcode) </div> </div> <div class="form-group"> @html.labelfor(model => model.country, "country", new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.dropdownlistfor(model => model.countryid, (ienumerable<selectlistitem>)viewbag.countries, new { @class = "form-control" }) @html.validationmessagefor(model => model.country) </div> </div> <div class="form-group"> @html.labelfor(model => model.isprimary, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.radiobuttonfor(model => model.isprimary, "true") yes @html.radiobuttonfor(model => model.isprimary, "false") no </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="create" class="btn btn-default" /> </div> </div> </div> }
most binder tries best find value id, , since it's not presented in view, take field has 'id" in it's name. try add hidden field id or create model view instead of passing domain object
Comments
Post a Comment