我一直在寻找一个解决方案,因为我的视图是使用列表模板中相应操作的添加视图创建的,它提供了一个例外 System.NullReferenceException:对象引用没有设置为一个实例对象。
Line 45: </tr>
Line 46:
Line 47: @foreach (var item in Model) {
Line 48: <tr>
Line 49: <td>
这是我的控制器功能
public ActionResult ViewCarpets()
{
IEnumerable<SelectListItem> Qualities = context.Qualities.Select(c => new SelectListItem
{
Value = c.Quality_Id.ToString(),
Text = c.QName
});
IEnumerable<SelectListItem> Suppliers = context.Suppliers.Select(c => new SelectListItem
{
Value = c.Supplier_Id.ToString(),
Text = c.SName
});
IEnumerable<SelectListItem> Designs = context.Design.Select(c => new SelectListItem
{
Value = c.Design_Id.ToString(),
Text = c.DName
});
ViewBag.Quality_Id = Qualities;
ViewBag.Supplier_Id = Suppliers;
ViewBag.Design_Id = Designs;
return View("ViewCarpets");
}
这是我的查看
@model IEnumerable<SOC.Models.Product>
@{
ViewBag.Title = "ViewCarpets";
}
<h2>View Carpets</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Design.DName)
</th>
<th>
@Html.DisplayNameFor(model => model.Quality.QName)
</th>
<th>
@Html.DisplayNameFor(model => model.Supplier.SName)
</th>
<th>
@Html.DisplayNameFor(model => model.PColor)
</th>
<th>
@Html.DisplayNameFor(model => model.PBorder_Color)
</th>
<th>
@Html.DisplayNameFor(model => model.PSKU)
</th>
<th>
@Html.DisplayNameFor(model => model.PLength)
</th>
<th>
@Html.DisplayNameFor(model => model.PWidth)
</th>
<th>
@Html.DisplayNameFor(model => model.PCost)
</th>
<th>
@Html.DisplayNameFor(model => model.IsSold)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Design.DName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Quality.QName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Supplier.SName)
</td>
<td>
@Html.DisplayFor(modelItem => item.PColor)
</td>
<td>
@Html.DisplayFor(modelItem => item.PBorder_Color)
</td>
<td>
@Html.DisplayFor(modelItem => item.PSKU)
</td>
<td>
@Html.DisplayFor(modelItem => item.PLength)
</td>
<td>
@Html.DisplayFor(modelItem => item.PWidth)
</td>
<td>
@Html.DisplayFor(modelItem => item.PCost)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsSold)
</td>
<td>
@Html.ActionLink("Edit", "EditCarpet", new { id = item.Product_Id }) |
@Html.ActionLink("Details", "CarpetDetails", new { id = item.Product_Id }) |
@Html.ActionLink("Delete", "DeleteCarpet", new { id = item.Product_Id })
</td>
</tr>
}
</table>
最后模型
namespace SOC.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
[Table("Product")]
public partial class Product
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Product()
{
Order_Details = new HashSet<Order_Details>();
}
public Product(Product product)
{
this.Quality_Id = product.Quality_Id;
this.Design_Id = product.Design_Id;
this.Supplier_Id = product.Supplier_Id;
this.PColor = product.PColor;
this.PBorder_Color = product.PBorder_Color;
this.PSKU = product.PSKU;
this.PLength = product.PLength;
this.PWidth = product.PWidth;
this.IsSold = product.IsSold;
}
[Key]
public int Product_Id { get; set; }
[Display(Name="Quality")]
public int Quality_Id { get; set; }
[Display(Name = "Design")]
public int Design_Id { get; set; }
[Display(Name = "Supplier")]
public int Supplier_Id { get; set; }
[Required]
[StringLength(20)]
[Display(Name = "Color")]
public string PColor { get; set; }
[Required]
[StringLength(20)]
[Display(Name = "Border Color")]
public string PBorder_Color { get; set; }
[Required]
[Display(Name = "Stock #")]
public string PSKU { get; set; }
[Display(Name = "Length")]
public decimal PLength { get; set; }
[Display(Name = "Width")]
public decimal PWidth { get; set; }
[Display(Name = "Cost")]
public decimal PCost { get; set; }
[Display(Name = "In Stock")]
public bool IsSold { get; set; }
public virtual Design Design { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Order_Details> Order_Details { get; set; }
public virtual Quality Quality { get; set; }
public virtual Supplier Supplier { get; set; }
}
}
答案 0 :(得分:1)
您没有构建模型,也没有调用接受模型的View()
重载,因此您的模型为空。
public ActionResult ViewCarpets()
{
IEnumerable<SelectListItem> Qualities = context.Qualities.Select(c => new SelectListItem
{
Value = c.Quality_Id.ToString(),
Text = c.QName
});
IEnumerable<SelectListItem> Suppliers = context.Suppliers.Select(c => new SelectListItem
{
Value = c.Supplier_Id.ToString(),
Text = c.SName
});
IEnumerable<SelectListItem> Designs = context.Design.Select(c => new SelectListItem
{
Value = c.Design_Id.ToString(),
Text = c.DName
});
ViewBag.Quality_Id = Qualities;
ViewBag.Supplier_Id = Suppliers;
ViewBag.Design_Id = Designs;
var products = GetProducts(); // whatever you need to do to get a list of products, database call etc.
return View("ViewCarpets", model: products);
}
答案 1 :(得分:0)
您没有将模型传递给视图:
return View("ViewCarpets");
应该是:
var model = new Your_model_class(); // Product for example
return View("ViewCarpets", model);
答案 2 :(得分:0)
非常感谢你们的帮助,我从@Dan D得到了一个提示,所以我改变了下面的动作方法,问题解决了
public ActionResult ViewCarpets()
{
//TODO: Implement View Carpets Logic
IEnumerable<SelectListItem> Qualities = context.Qualities.Select(c => new SelectListItem
{
Value = c.Quality_Id.ToString(),
Text = c.QName
});
IEnumerable<SelectListItem> Suppliers = context.Suppliers.Select(c => new SelectListItem
{
Value = c.Supplier_Id.ToString(),
Text = c.SName
});
IEnumerable<SelectListItem> Designs = context.Design.Select(c => new SelectListItem
{
Value = c.Design_Id.ToString(),
Text = c.DName
});
var model = new Product();
ViewBag.Quality_Id = Qualities;
ViewBag.Supplier_Id = Suppliers;
ViewBag.Design_Id = Designs;
List<Product> product = context.Products.ToList();
return View(product);
}