c# - MVC Validation on Production Site Fails -


on localhost validation user's billing address works, before submitted. once pushed project production. validation not initiate , receive following error "sequence contains no elements. points controller "model.address = db.addresses.where(a => a.addressid == model.addressid).single();".

screenshot of code causing error

view

@using (html.beginform()) { @html.validationsummary(true)  @html.hiddenfor(m => m.userid) @html.hiddenfor(m => m.acceptsms) @html.hiddenfor(m => m.isactive) @html.hiddenfor(m => m.lastlogindate)  @html.hiddenfor(m => m.addressid)  <div class="editor-label">     name </div> <div class="editor-field">     @html.editorfor(m => m.firstname)     @html.validationmessagefor(m => m.firstname) </div>  <div class="editor-field">     @html.editorfor(m => m.lastname)     @html.validationmessagefor(m => m.lastname) </div>  <div class="editor-label">     @html.labelfor(m => m.phone) </div> <div class="editor-field">     @html.textboxfor(m => m.phone)     @html.validationmessagefor(m => m.phone) </div>  <div class="editor-label">     @html.labelfor(m => m.mobile) </div> <div class="editor-field">     @html.textboxfor(m => m.mobile)     @html.validationmessagefor(m => m.mobile) </div>  <div class="editor-label">     @html.labelfor(model => model.dateofbirth) </div> <div class="editor-field">     @html.editorfor(model => model.dateofbirth)     @html.validationmessagefor(model => model.dateofbirth) </div>  <div class="editor-label">     @html.labelfor(model => model.gender) </div> <div class="editor-field">     @html.radiobutton("gender", "male") male     @html.radiobutton("gender", "female") female     @html.validationmessagefor(m => m.gender) </div>  <div class="editor-label">     @html.labelfor(model => model.addressid, "address")     @html.validationmessagefor(m => m.addressid) </div> 

controller

[httppost]     public actionresult edit(userprofile model)     {          model.address = db.addresses.firstordefault(a => a.addressid == model.addressid);          // check verify phone number length correct         // (when using input mask, shouldn't problem)         if (model.phone.length != 14)         {             modelstate.addmodelerror("", "please enter valid phone number");         }          if (model.mobile != null && model.mobile.length != 14)         {             modelstate.addmodelerror("", "please enter valid mobile number");         }          // checks make sure user 18 years old         if (model.dateofbirth.date > datetime.now.addhours(2).date.addyears(-18))         {             modelstate.addmodelerror("", "you must @ least 18 years of age");         }          if (modelstate.isvalid)         {             db.entry(model).state = entitystate.modified;             db.savechanges();             return redirecttoaction("myaccount", "account");         }          return view(model);     } 

model

namespace xyz.models.domain {public class userprofilemetadata {     [hiddeninput(displayvalue = false)]     [display(name = "user id")]     public int userid { get; set; }      [required(errormessage = "please enter first name")]     [display(name = "first name")]     public string firstname { get; set; }      [required(errormessage = "please enter last name")]     [display(name = "last name")]     public string lastname { get; set; }      [display(name = "date of birth")]     [displayformat(dataformatstring = "{0:d}", applyformatineditmode = true)]     public datetime dateofbirth { get; set; }      [required(errormessage = "please enter phone number")]     public string phone { get; set; }      //[required(errormessage = "please enter mobile number")]     public string mobile { get; set; }      [required(errormessage = "please select gender")]     public string gender { get; set; }      [display(name = "address")]     public nullable<int> addressid { get; set; }      [required(errormessage = "please select activation status")]     [display(name = "is active")]     public bool isactive { get; set; } } 

i'm guessing you're seeing error thrown here:

model.address = db.addresses.where(a => a.addressid == model.addressid).single(); 

try .singleordefault().

also check if db.addresses not empty

if (model.address != null && db.addresses.any()) 

edit: try this

model.address = db.addresses.firstordefault(a => a.addressid == model.addressid); 

edit2: assign model.address existing address if it's not new address:

if (model.addressid != null && db.addresses.any(a => a.addressid == model.addressid))     model.address = db.addresses.first(a => a.addressid == model.addressid); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -