Much
Much

Reputation: 165

How to display an id item from another controller to other controller in asp.net mvc

i already pass an id from room to booking controller,

 <button type="button" onclick="location.href='@Url.Action("Create", "Bookings", new { id = item.RoomID })'" class="btn btn-info">Book</button>

now i want to display that id, to other controller after click book button, here is the image i want to display this item

now, i want to past to another controlle which display the id as text, and show here, but the image didnt show the details. enter image description here

here is my code in booking controller ,

 public ActionResult Create(int id)
    {
        var bookings = db.Bookings.Include(b => b.Room).Include(b => b.Register);



        ViewBag.RoomID = new SelectList(db.Rooms, "RoomID", "RoomType");
        ViewBag.CustomerID = new SelectList(db.Registers, "id", "username");
        return View();
    }

what i should do to display the details of room id in booking controller?

here is the code for view, enter image description here

Upvotes: 0

Views: 1333

Answers (2)

Tetsuya Yamamoto
Tetsuya Yamamoto

Reputation: 24957

There are some possible concerns in your code:

1) Avoid using viewmodel property name as ViewBag property name, as this may cause confusion between both of them.

2) You're not populate anything inside DropDownList helper by setting second parameter to null value, use ViewBag properties containing SelectList or List<SelectListItem> to populate it.

3) Use strongly-typed DropDownListFor for each viewmodel properties.

Based from those points above, the controller action should be like this:

public ActionResult Create(int id)
{
    var bookings = db.Bookings.Include(b => b.Room).Include(b => b.Register);

    var model = new ViewModel();

    // use query to get both selected IDs
    model.RoomId = bookings.Where(...).Select(x => x.RoomId).FirstOrDefault();
    model.CustomerId = bookings.Where(...).Select(x => x.CustomerId).FirstOrDefault();

    ViewBag.RoomList = new SelectList(db.Rooms, "RoomID", "RoomType");
    ViewBag.CustomerList = new SelectList(db.Registers, "id", "username");
    return View(model);
}

And both dropdownlists should use strongly-typed helper as in example below:

@Html.DropDownListFor(model => model.RoomId, ViewBag.RoomList as SelectList, "-- Select Room --", new { @class = "form-control" })

@Html.DropDownListFor(model => model.CustomerId, ViewBag.CustomerList as SelectList, "-- Select Customer --", new { @class = "form-control" })

Note: Better to populate option lists inside viewmodel properties which have SelectList/List<SelectListItem> type and pass it directly to view:

Model

public List<SelectListItem> RoomList { get; set; }

public List<SelectListItem> CustomerList { get; set; }

Controller Action

public ActionResult Create(int id)
{
    var bookings = db.Bookings.Include(b => b.Room).Include(b => b.Register);

    var model = new ViewModel();

    // use query to get both selected IDs
    model.RoomId = bookings.Where(...).Select(x => x.RoomId).FirstOrDefault();
    model.CustomerId = bookings.Where(...).Select(x => x.CustomerId).FirstOrDefault();

    model.RoomList = db.Rooms.Select(x => new SelectListItem { Text = x.RoomType, Value = x.RoomID }).ToList();
    model.CustomerList = db.Registers.Select(x => new SelectListItem { Text = x.username, Value = x.id }).ToList();
    return View(model);
}

View

@Html.DropDownListFor(model => model.RoomId, Model.RoomList, "-- Select Room --", new { @class = "form-control" })

@Html.DropDownListFor(model => model.CustomerId, Model.CustomerList, "-- Select Customer --", new { @class = "form-control" })

Upvotes: 1

Tuan Zaidi
Tuan Zaidi

Reputation: 343

You can set selected value from controller.

    public ActionResult Create(int id)
    {
        var bookings = db.Bookings.Include(b => b.Room).Include(b => b.Register);

        ViewBag.RoomID = new SelectList(db.Rooms, "RoomID", "RoomType", id);
        ViewBag.CustomerID = new SelectList(db.Registers, "id", "username");
        return View();
    }

Upvotes: 0

Related Questions