【问题标题】:How to display an id item from another controller to other controller in asp.net mvc如何在asp.net mvc中将id项从另一个控制器显示到另一个控制器
【发布时间】:2019-05-13 06:38:13
【问题描述】:

我已经将一个 id 从房间传递给预订控制器,

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

现在我想在单击预订按钮后向其他控制器显示该 ID,这是图像

现在,我想传递到另一个将 id 显示为文本的控件,并在此处显示,但图像没有显示详细信息。

这是我在预订控制器中的代码,

 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();
    }

我应该怎么做才能在预订控制器中显示房间 ID 的详细信息?

这里是查看代码,

【问题讨论】:

  • 您可以创建一个视图模型并在返回视图时传递所需的值,但我想查看包含下拉列表和日期文本框的视图代码,如图所示。
  • @TetsuyaYamamoto 看到我的更新,已经添加

标签: c# jquery controller parameter-passing


【解决方案1】:

您的代码中可能存在一些问题:

1) 避免使用 viewmodel 属性名称作为 ViewBag 属性名称,因为这可能会导致两者混淆。

2) 通过将第二个参数设置为空值,您不会在 DropDownList 助手中填充任何内容,请使用包含 SelectListList&lt;SelectListItem&gt;ViewBag 属性来填充它。

3) 为每个视图模型属性使用强类型 DropDownListFor

基于以上几点,控制器动作应该是这样的:

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);
}

并且两个下拉列表都应该使用强类型帮助器,如下例所示:

@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" })

注意:最好在具有SelectList/List&lt;SelectListItem&gt; 类型的视图模型属性中填充选项列表并将其直接传递给视图:

型号

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

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

控制器动作

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);
}

查看

@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" })

【讨论】:

  • 但我想在文本中显示值,而不是下拉列表,知道如何获取它吗?
【解决方案2】:

您可以从控制器中设置选定的值。

    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();
    }

【讨论】:

    猜你喜欢
    • 2018-08-16
    • 2020-11-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多