locked
ASP.NET working with DB-forms using LINQ RRS feed

  • Question

  • User48808186 posted

    I'm learing ASP.NET. So here is the problem:

    I have 2 tables in DB. One is "kupac" and the other is "racun". Both have some attributes, but important thing is that "racun" has FK "kupac_id" that tells who the racun belongs to. I have following code for create "racun", generated by ASP.NET.

    // GET: racuns/Create
            public ActionResult Create(int? id)
            {
                ViewBag.customer_id = new SelectList(db.kupac, "id_kupac", "naziv");
                return View();
            }
    
            // POST: racuns/Create
            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Create([Bind(Include = "id_racun,dv_racun,status,dv_placanja,kupac_id")] racun racun)
            {
                if (ModelState.IsValid)
                {
                    db.racuns.Add(racun);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
    
                ViewBag.kupac_id = new SelectList(db.kupacs, "id_kupac", "naziv", racun.kupac_id);
                return View(racun);
            }

    This way I have form with fields to fulfill, and for FK customer_id I have dropdown menu to select one of existing Customers, which works fine.

    In list of customers, I want to put by every Customer name a link which takes me to create bill form that same as previous one, but without dropdown menu, instead of that, customer_id should be sent by link, i managed to get that by this code.

    @Html.ActionLink("Add bill", "Create", "Bills", new { id = item.id_customer }, null)

    Problem is, how do I insert that ID in DB with all other information?

    Please help. If u need any additional info, just ask.

    This is how look Create action in Controller

    // GET: racuns/Create
            public ActionResult Create(int? id)
            {
                ViewBag.kupac_id = new SelectList(db.kupacs, "id_kupac", "naziv");
                return View();
            }
    
            // POST: racuns/Create
            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Create(int? id,[Bind(Include = "id_racun,dv_racuna,status,dv_placanja,kupac_id")] racun racun)
            {
                if (ModelState.IsValid)
                {
                    db.racuns.Add(racun);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
             
                ViewBag.kupac_id = new SelectList(db.kupacs, "id_kupac", "naziv", racun.kupac_id);
                
                return View(racun);
            }



    PROBLEM SOLVED
    I created new controller and it worked o.O
    Tuesday, December 15, 2015 12:32 PM

Answers

  • User-271186128 posted

    Hi BinHex,

    Glad to see you have solve the problem by yourself, if you have any further questions about asp.net, please feel free to post your problem to this forum.

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 16, 2015 2:47 AM