我在asp.net中使用mvc。我想要做的是:我有一个项目控制器的视图(索引)和一个Order控制器的创建方法。索引视图代码是:
查看
@model IEnumerable<MvcHotel.Models.Items>
@{
ViewBag.Title = "Index";
//var item = Model;
}
//other markup
<div class="row">
@foreach (var item in Model)
{
<div class="col-md-2">
<img class="img-responsive img-rounded" src="/Item/RetrieveImage/@item.Id" />
<div>
<p>@item.ItemName</p>
<p>Rs.<span>@item.Price</span>
<a href="?/order/create/@item.Id"><span class="pull-right">Order Now </span></a>
<p class="text-warning"><span>@item.Rating stars</span></p>
</div>
</div>
}
</div>
控制器
[HttpGet]
public ActionResult Create(int id)
{
db = new DBContext();
//var categories = new SelectList(new[] { "Rice","Karachi","Fast Food","Drinks", "Shakes" });
ViewBag.Categories = new SelectList(db.Categories, "CategoryID", "CategoryName");
return View();
}
[Authorize]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(MvcHotel.Models.Order order)
{
//HttpPostedFileBase file = Request.Files["ImageData"];
//int i = UploadImageInDataBase(file, order);
if (ModelState.IsValid)
{
db = new DBContext();
db.Orders.Add(order);
db.SaveChanges();
return RedirectToAction("Index");
}
return Index();
}
现在我想传递项目ID,以便项目名称及其类别自动显示在订单的创建(视图)的下拉列表中。请帮帮我
答案 0 :(得分:1)
创建一个模型类来为您的视图提供服务:
public class ItemModel
{
public int ItemId { get; set; }
public List<Category> Categories { get; set; }
public SelectedCategoryId { get; set; }
}
创建一个实例并在控制器中填充它,然后将其放入View
操作中的Create(int id)
方法:
return View(instanceHere);
然后在您的视图中使用它:
@model ItemModel