0

I have 2 tables tbl_computer and tbl_computerperipheral I need a editor view which consists of data from both tables.

How Can I get 2 tables in a single view so that I can insert data into 2 tables at once.

Thanx

  • are you getting data from two table, and populate in single view? yes or no – JegsVala Mar 14 '14 at 04:58
  • Hello and welcome to StackOverflow. I don't think that your question is very clear. Please describe your problem in full with as much detail as possible. Please read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) and [Writing the perfect question](http://tinyurl.com/so-hints). – Rowan Freeman Mar 14 '14 at 05:06

2 Answers2

0

For your Better Reference just have a look to ::

How to Combine two models into a single model and pass it to view using asp.net MVC razor

and then on Form submit on server side (i.e. into controller's action) save the data coming from view in the form like::

public ActionResult Save(CommonViewModel common)
    {
        var FirstModel = new FirstModel();
        FirstModel = common.FirstModel;
        db.Entry(FirstModel).State = EntityState.Added;

        var SecondModel = new SecondModel();
        SecondModel = common.SecondModel;
        db.Entry(SecondModel).State = EntityState.Added;
        db.SaveChanges();
    }

May be this answer will be helpful to get answer of your Query.

Community
  • 1
  • 1
Rahul
  • 2,309
  • 6
  • 33
  • 60
  • Thanx for the reply Rahul RJ .. When I use the above code I cannot get data. The code FirstModel = common.FirstModel doesnot give all the data. I need to Add all fields one by one. Is there any other way – user3405577 Mar 17 '14 at 05:25
0

[HttpPost]

    public ActionResult Edit(CompPeripheral cp, int c_id,int em_id,int asset_id)
    {
        if (ModelState.IsValid)
        {
            cp.Compconfig.c_id = c_id;
            cp.Compconfig.em_id = em_id;
            cp.Compconfig.asset_id = asset_id;
            db.tbl_compconfig.Add(cp.Compconfig);
            db.SaveChanges();
            var id = db.tbl_compconfig.Max(a => a.comp_id);
            cp.Comperipheral.comp_id = id;
            //saving Comp Peripheral in database
            db.tbl_comperipheral.Add(cp.Comperipheral);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.asset_id = new SelectList(db.tbl_assetm, "asset_id", "asset_name", cp.Compconfig.asset_id);
        ViewBag.em_id = new SelectList(db.tbl_employee, "em_id", "em_fullname", cp.Compconfig.em_id);
        ViewBag.c_id = new SelectList(db.tbl_client, "c_id", "c_name", cp.Compconfig.c_id);
        return View(cp);
    }