Results 1 to 2 of 2

Thread: display customer name in drop down list

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2007
    Location
    Karachi
    Posts
    551

    display customer name in drop down list

    i have two controller customer and order in mvc Application ..I wnat to show customer in drop down list in order controller when click on order create ..or click on following url .http://localhost:1957/order/Create

    any help ..

    here is sample code

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using MVCStoreProcedure.Models;

    namespace MVCStoreProcedure.Controllers
    {
    public class OrderController : Controller
    {
    private CustomerEntities1 db = new CustomerEntities1();

    //
    // GET: /Order/

    public ActionResult Index()
    {
    // return View(db.CustomerTBLs



    return View(db.OrderTBLs.ToList());
    }

    //
    // GET: /Order/Details/5

    public ActionResult Details(int id = 0)
    {
    OrderTBL ordertbl = db.OrderTBLs.Find(id);
    if (ordertbl == null)
    {
    return HttpNotFound();
    }
    return View(ordertbl);
    }

    //
    // GET: /Order/Create

    public ActionResult Create()
    {

    return View();
    }

    //
    // POST: /Order/Create

    [HttpPost]
    public ActionResult Create(OrderTBL ordertbl)
    {
    if (ModelState.IsValid)
    {
    db.OrderTBLs.Add(ordertbl);
    db.SaveChanges();
    return RedirectToAction("Index");
    }

    return View(ordertbl);
    }

    //
    // GET: /Order/Edit/5

    public ActionResult Edit(int id = 0)
    {
    OrderTBL ordertbl = db.OrderTBLs.Find(id);
    if (ordertbl == null)
    {
    return HttpNotFound();
    }
    return View(ordertbl);
    }

    //
    // POST: /Order/Edit/5

    [HttpPost]
    public ActionResult Edit(OrderTBL ordertbl)
    {
    if (ModelState.IsValid)
    {
    db.Entry(ordertbl).State = EntityState.Modified;
    db.SaveChanges();
    return RedirectToAction("Index");
    }
    return View(ordertbl);
    }

    //
    // GET: /Order/Delete/5

    public ActionResult Delete(int id = 0)
    {
    OrderTBL ordertbl = db.OrderTBLs.Find(id);
    if (ordertbl == null)
    {
    return HttpNotFound();
    }
    return View(ordertbl);
    }

    //
    // POST: /Order/Delete/5

    [HttpPost, ActionName("Delete")]
    public ActionResult DeleteConfirmed(int id)
    {
    OrderTBL ordertbl = db.OrderTBLs.Find(id);
    db.OrderTBLs.Remove(ordertbl);
    db.SaveChanges();
    return RedirectToAction("Index");
    }

    protected override void Dispose(bool disposing)
    {
    db.Dispose();
    base.Dispose(disposing);
    }
    }
    }
    There is no achievement without goals

  2. #2
    Junior Member valleteclark12's Avatar
    Join Date
    Mar 2015
    Location
    Best Cordless Drill & Tool Kit Buying Guide
    Posts
    16

    Re: display customer name in drop down list

    Have a lot of way to display dropdownlist, below is one:

    In code behide:
    Code:
    public ActionResult Index()
            {
                var lst = new List<ObjectInfo>();
                for (int i = 1; i < 10; i++)
                {
                    var obj = new ObjectInfo();
                    obj.Id = i.ToString();
                    obj.Name = "Name-" + i.ToString();
                    lst.Add(obj);
                }
    
                var selectlist = new SelectList(lst, "Id", "Name", "4");        
                return View(lst);
            }
    In code CSHTML:
    Code:
    @Html.DropDownList("Customer",new SelectList(Model,"ID","Name","2"),"----")
    I hope it helps you….Happy coding

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width