c# - Insert data and display on the same view in mvc -


enter image description herei want insert data , show inserted data same view. can use partial view. please help, here giving error @ time of insertion.

error screenshot

entity class

using system; using system.collections.generic; using system.linq; using system.web; using system.componentmodel.dataannotations; namespace webapplication7.models {     public class class1     {         [key]         public int id { get; set; }         [required(errormessage="name plz")]         public string name { get; set; }         public string address { get; set; }         public string email { get; set; }         public string phone { get; set; }     } }    

data layer class

public class dlayer {     sqlconnection con = new sqlconnection(configurationmanager.connectionstrings\["connection"\].connectionstring);      public dataset getalldata()     {         string query = "select * employee";         sqlcommand cmd = new sqlcommand(query,con);         //cmd.commandtext = "select * employee";         //cmd.commandtype = commandtype.text;         //cmd.connection = con;         sqldataadapter da = new sqldataadapter(cmd);         dataset ds = new dataset();                da.fill(ds);         return ds;     }      public string insert(class1 cl1)     {         string str = "";         sqlcommand cmd = new sqlcommand();         cmd.commandtext = "insert employee(name,address,email,phone) values(@name,@address,@email,@phone)";         cmd.parameters.addwithvalue("@name", cl1.name);         cmd.parameters.addwithvalue("@address", cl1.address);         cmd.parameters.addwithvalue("@email", cl1.email);         cmd.parameters.addwithvalue("@phone", cl1.phone);         cmd.commandtype = commandtype.text;         cmd.connection = con;         con.open();         int result = cmd.executenonquery();         con.close();         return str = convert.tostring(result);     } } 

default controller.cs

using system; using system.collections.generic; using system.linq; using system.web; using system.web.mvc; using webapplication7.models; using webapplication7.datalayer; using system.data; namespace webapplication7.controllers {     public class defaultcontroller : controller     {          public actionresult index()     {         dlayer dl = new dlayer();         dataset ds = new dataset();         datatable dt = new datatable();         dt = dl.getalldata();         list<class1> products = new list<class1>();           foreach (datarow dr  in dt.rows)         {              products.add(new class1() { id = int.parse(dr[0].tostring()), name = dr[1].tostring(), phone = dr[2].tostring(), email = dr[3].tostring() });             }             return view(products);     }          public actionresult insert()         {             return view();         }          [httppost]         public actionresult insert(class1 cls)         {             dlayer dl = new dlayer();             string = dl.insert(cls);            }     } } 

you can return view("index") return index view , remove [httppost]. ignore else.


Comments

Popular posts from this blog

php - isset function not working properly -

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -