asp.net mvc - MVC 5.0 <Format Exception> -
in mvc application "twentyfifth", model :
public class student { public string studentname { get; set; } public int studentid { get; set; } public string fathername { get; set; } }
a method support-class in same project :
public void editsupport(int id, student std2) { sqlconnection con = new sqlconnection("data source=my;initial catalog=db;integrated security=true"); sqlcommand com = new sqlcommand("update studentt set studentname='"+ std2.studentname + "', fathername='" + std2.fathername + "' studentid=" + id + "", con); con.open(); com.executenonquery(); }
and finally, controller goes :
[httppost] [actionname("edit")] public actionresult edit_post(int id, formcollection formcollector) { student std = new student(); std.studentid = id; std.studentname = formcollector["studentname"].tostring(); std.fathername = formcollector["fathername"].tostring(); bussinessndatalayer bl = new bussinessndatalayer(); bl.editsupport(id, std); return redirecttoaction("index"); }
question : why there format exception when change controller ->
std.studentid = convert.toint32(formcollector["studentid"]);
note : @ break point found string value in std.studentid comma(,) reply.
the default model binder handle of you. try changing code model binder conversion you:
public actionresult edit_post(int id, formcollection formcollector, int studentid) { ... std.studentid = studentid; ... }
it best practice never access form collection directly , use typed viewmodels , action method params instead.
Comments
Post a Comment