我正在做一个关于中国食品订购系统的项目,我希望在另一个页面的视图中显示所选项目。我正在尝试使用会话,但我不断得到不同的错误,例如“传递到字典中的模型项是类型”和“对象引用未设置为对象的实例”。如何选择数据并在不同的视图中显示?项目链接到数据库,DishID和其他实例应通过数据库连接。
控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KinaRest.Models;
using KinaRest.View_Models;
using KinaRest.Infrastructure;
namespace KinaRest.Controllers
{
public class TakeawayController : Controller
{
private Basket basket = new Basket();
[ChildActionOnly]
public ActionResult BasketItemsList()
{
Basket basket = new Basket();
return View("_Basket", basket.Items);
}
// GET: /Takeaway/
private ChinaContext db = new ChinaContext();
[HttpGet]
public ActionResult Index()
{
return View(db.Dish.ToList());
}
[HttpPost]
public ActionResult Index(BasketItem basketItem)
{
//Repository repository = new Repository();
if (Session["Basket"] == null)
{
Session["Basket"] = new Basket();
}
else
{
basket = (Basket)Session["Basket"];
}
basket.AddItem(basketItem);
return View("Test");
}
}
}
模型1:篮子
namespace KinaRest.Infrastructure
{
public class Basket
{
private List<BasketItem> items = new List<BasketItem>();
public List<BasketItem> Items { get { return items; } }
public void AddItem(BasketItem bi) {
items.Add(bi);
}
}
}
模型2:BasketItem
namespace KinaRest.Infrastructure
{
public class BasketItem
{
public int DishId {get; set; }
public int Number { get; set; }
public decimal Price { get; set; }
}
}
在此视图中,我们尝试使用表单收集数据,例如ID,数量(数量)和价格:
@model IEnumerable<KinaRest.Models.Dish>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
<div class="form-group">
@using (Html.BeginForm())
{
foreach (var item in Model)
{
<div class="col-md-10">
@Html.DisplayFor(modelItem => item.DishId)
</div>
<div class="col-md-10">
@Html.DisplayFor(modelItem => item.Title)
</div>
<div class="col-md-10">
@Html.DisplayFor(modelItem => item.Price)
</div>
<div class="col-md-10">
@Html.DisplayFor(modelItem => item.Description)
</div>
<div class="col-md-10">
@Html.DisplayFor(modelItem => item.Type.Type1)
</div>
<img src="@Url.Content("~/content/images/" + item.Image)" />
@Html.EditorFor(model => item.Number, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => item.Number, "", new { @class = "text-danger" })
<input type="submit" value="Tilføj" class="btn btn-default" />
}
}
</div>
在此视图中,我们尝试访问会话数据:
@model List<KinaRest.Infrastructure.BasketItem>
@{
ViewBag.Title = "Test";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Test</h2>
<ul>
@foreach (var item in Model)
{
<li>
@Html.DisplayFor(modelItem => item.Number)
</li>
}
</ul>
是的,我是MVC的新手,我还在学习。希望你们能帮忙。
答案 0 :(得分:1)
[HttpGet]
public ActionResult Order(int id)
{
Dish dish = db.Dish.Single(d=> d.DishId == id); //database call here
BasketItem basketItem = new BasketItem(){DishId = dish.?, Number = dish.?, Price = dish.Price }; // create a new BasketItem
Basket basket; //create reference outside if block
//Repository repository = new Repository();
if (Session["Basket"] == null)
{
basket = new Basket();
Session["Basket"] = basket;
}
else
{
basket = (Basket)Session["Basket"];
}
basket.AddItem(basketItem);
return View("Test", basket.Items); //or return View(((Basket)Session["Basket"]).Items); // give the view the data
}
取出班级篮子。据我所知,每次处理请求时都会实例化控制器
查看订购
@model IEnumerable<KinaRest.Models.Dish>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
<table>
<tr><th></th><th>Dish</th><th></th></tr>
@foreach(var item in Model)
{
<tr><td><img src="@Url.Content("~/content/images/" + item.Image)" /></td><td>@html.displayfor(item => item.Name)</td><td>@Html.ActionLink("Add", "Order", "TakeAway", new {id = item.DishId})</td></tr>
}
</table>