c# - Passing List of Data to Other Controller -


so have action method in controller data csv file uploaded through web

i want pass data insert controller data csv automatically inserted tables in db , pass view

i'm using csv helper, mvc

        public actionresult importcsv(httppostedfilebase file, int compid)         {             var compname = db.couriercompanies.find(compid);              string path = null;              list<myviewmodel> csvd = new list<myviewmodel>();              try             {                 if(file.contentlength > 0)                 {                     var filename = path.getfilename(file.filename);                     path = appdomain.currentdomain.basedirectory + "upload\\" + filename;                     file.saveas(path);                      var csv = new csvreader(new streamreader(path));                     var invocsv = csv.getrecords<importcsv>();                      foreach(var in invocsv)                     {                         myviewmodel icsv = new myviewmodel();                          icsv.custid = i.cust_id;                         icsv.fullname = i.fullname;                         icsv.custcomp = i.company;                         icsv.invoiceno = i.rec_no;                         icsv.insertdate = datetime.parse(i.doc_dt);                         icsv.road = i.w_addr1;                         icsv.city = i.w_city;                         icsv.zip = i.w_zip;                         icsv.phone = i.w_phone;                         icsv.status = "belumterkirim";                         icsv.compid = compid;                         icsv.compname = compname.compname;                         icsv.statdate = datetime.now;                          csvd.add(icsv);                     }                 }             }             catch             {                 viewdata["error"] = "upload failed";             }              return view();         } 

insert controller

public actionresult create( myviewmodel model, int compid, httppostedfilebase file)         {             if (file != null)             {                 using (memorystream ms = new memorystream())                 {                     file.inputstream.copyto(ms);                     model.image = ms.getbuffer();                 }             }              var cust = new customer()             {                 custid = model.custid,                 phone = model.phone,                 custcomp = model.custcomp,                 fullname = model.fullname             };              var addrdet = new addrdetail()             {                 road = model.road,                 city = model.city,                 zipcode = model.zip             };              var invoice = new invoice()             {                 invoiceno = model.invoiceno             };              var stat = new status()             {                 status1 = model.status,                 statdate = model.statdate,                 ket = model.ket             };              var image = new models.image()             {                 image1 = model.image             };              var detail = new detailpengiriman()             {                 namapenerima = model.namapenerima,                 statuspenerima = model.statuspenerima,                 trdate = model.trdate,                 insertdate = model.insertdate             };              if (modelstate.isvalid )             {                 //customer                 db.customers.add(cust);                 detail.custid = cust.custid;                 invoice.custid = cust.custid;                  //addrdet                 db.addrdetails.add(addrdet);                 cust.addrdetid = addrdet.addrdetid;                  //invoice                 db.invoices.add(invoice);                 stat.invoiceno = invoice.invoiceno;                 image.invoiceno = invoice.invoiceno;                 detail.invoiceno = invoice.invoiceno;                  //status                 db.status.add(stat);                 detail.statusid = stat.statusid;                  ////image                 db.images.add(image);                 detail.imageid = image.imageid;                  //detail                 detail.compid = compid;                 db.detailpengirimen.add(detail);                  db.savechanges();                  return redirecttoaction("index", new { compid = detail.compid});             }             return view();         } 

you can abstract business logic in class , instantiate inside csv action.

this way can call methods inserting customers both actions!


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -