OK,在这里,Google和其他编程网站搜索和搜索之后,是时候问我的第一个问题了。
我有一个视图 Index.cshtml,我需要两个模型,所以我创建了一个 ViewModel ImageViewModel.cs,两个子模型 ImageModel.cs 和 ProductModel.cs 并有一个控制器 ImageController.cs。
对于我的一生,我无法让 IEnumerable 工作,我应该得到一组数据,一个可能有一个项目(产品),另一个可能有很多(图像)。图像与 productID 相关联。
玩过之后,我现在到了一个点,在视图中,@Html.DisplayFor(modelItem => item.imageTitle) 会抛出一个错误,即 imageTitle 不存在。
如果我恢复散布在各处的众多 IEnumerable 项目中的任何一个,我会继续获取传入的 ImageViewModel 不是 IEnumerable 并且视图期望它是。
至于它是什么,这里是完整的代码清单:
索引.cshtml
@model IEnumerable<JustAdminIt.Areas.JBI.ViewModels.ImageViewModel>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
Image Title
</th>
<th>
Image Excerpt
</th>
<th>
Image Description
</th>
<th>
Image
</th>
<th>
Product ID
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Image.imageTitle)
</td>
<td>
@Html.DisplayFor(modelItem => item.Image.imageExcerpt)
</td>
<td>
@Html.DisplayFor(modelItem => item.Image.imageDescription)
</td>
<td>
<img src="@Url.Content("~/Content/images/product/")@Html.DisplayFor(modelItem => item.Image.imageURL)" alt="Product Image" width="150" />
</td>
<td>
@Html.DisplayFor(modelItem => item.Product.productID)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Image.imageID }) |
@Html.ActionLink("Details", "Details", new { id = item.Image.imageID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Image.imageID })
</td>
</tr>
}
</table>
产品模型.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Web.Mvc;
namespace JustAdminIt.Areas.JBI.Models
{
public class Product
{
[Key]
public int productID { get; set; }
[Required]
[DisplayName("Product Name")]
public string productName { get; set; }
[Required]
[AllowHtml]
[DisplayName("Product Excerpt")]
public string productExcerpt { get; set; }
[Required]
[AllowHtml]
[DisplayName("Product Description")]
public string productDescription { get; set; }
[DisplayName("Mark As Active")]
public bool productActive { get; set; }
[DisplayName("Product Add Date")]
public DateTime? productAddDate { get; set; }
[DisplayName("Product Inactive Date")]
public DateTime? productInactiveDate { get; set; }
}
}
图像模型.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Web.Mvc;
namespace JustAdminIt.Areas.JBI.Models
{
public class Image
{
[Key]
public int imageID { get; set; }
[Required]
public string imageTitle { get; set; }
[AllowHtml]
public string imageExcerpt { get; set; }
[AllowHtml]
public string imageDescription { get; set; }
[AllowHtml]
public string imageURL { get; set; }
[Required]
public int productID { get; set; }
}
}
图像控制器.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JustAdminIt.Areas.JBI.Models;
using JustAdminIt.Areas.JBI.ViewModels;
using JustAdminIt.Areas.JBI.DAL;
using System.IO;
namespace JustAdminIt.Areas.JBI.Controllers
{
public class ImageController : Controller
{
private ImageContext db = new ImageContext();
private ProductContext pb = new ProductContext();
//
// GET: /JBI/Image/
public ViewResult Index()
{
//attempt new things
int id = 1;
IEnumerable<Image> image = db.Image.TakeWhile(x => x.productID == id);
IEnumerable<Product> product = pb.Product.TakeWhile(x => x.productID == id);
ImageViewModel piViewModel = new ImageViewModel(image, product);
return View(piViewModel);
//return View(db.Image.ToList());
}
//
// GET: /JBI/Image/Details/5
public ViewResult Details(int id)
{
Image image = db.Image.Find(id);
return View(image);
}
//
// GET: /JBI/Image/Create
public ActionResult Create()
{
return View();
}
//
// POST: /JBI/Image/Create
//this simply populates the DB values, but does not handle the file upload
[HttpPost]
public ActionResult Create(Image image)
{
if (ModelState.IsValid)
{
db.Image.Add(image);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(image);
}
[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase Filedata)
{
// Verify that the user selected a file
if (Filedata != null && Filedata.ContentLength > 0)
{
// extract only the fielname
var fileName = Path.GetFileName(Filedata.FileName);
// store the file inside ~/App_Data/uploads folder
var path = Path.Combine(Server.MapPath("~/Content/images/product/"), fileName);
Filedata.SaveAs(path);
}
// redirect back to the index action to show the form once again
return RedirectToAction("Index");
}
//
// GET: /JBI/Image/Edit/5
public ActionResult Edit(int id)
{
Image image = db.Image.Find(id);
return View(image);
}
//
// POST: /JBI/Image/Edit/5
[HttpPost]
public ActionResult Edit(Image image)
{
if (ModelState.IsValid)
{
db.Entry(image).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(image);
}
//
// GET: /JBI/Image/Delete/5
public ActionResult Delete(int id)
{
Image image = db.Image.Find(id);
return View(image);
}
//
// POST: /JBI/Image/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Image image = db.Image.Find(id);
db.Image.Remove(image);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
ImageViewModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Web.Mvc;
using JustAdminIt.Areas.JBI.Models;
namespace JustAdminIt.Areas.JBI.ViewModels
{
public class ImageViewModel
{
public IEnumerable<ImageViewModel> Image { get; set; }
public IEnumerable<ImageViewModel> Product { get; set; }
public ImageViewModel(IEnumerable<Image> image, IEnumerable<Product> product)
{
IEnumerable<Image> Image = image;
IEnumerable<Product> Product = product;
}
}
}