Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Net MVC 3,使用ViewModel,错误是ViewModel不可IEnumerable_C#_Asp.net Mvc 3_Viewmodel_Ienumerable - Fatal编程技术网

C# Net MVC 3,使用ViewModel,错误是ViewModel不可IEnumerable

C# Net MVC 3,使用ViewModel,错误是ViewModel不可IEnumerable,c#,asp.net-mvc-3,viewmodel,ienumerable,C#,Asp.net Mvc 3,Viewmodel,Ienumerable,好了,在这里搜索了又搜索,谷歌和其他编程网站之后,是时候问我的第一个问题了 我有一个视图Index.cshtml,需要两个模型,所以我创建了一个ViewModel,ImageViewModel.cs,两个子模型ImageModel.cs和ProductModel.cs,还有一个控制器ImageController.cs 在我的一生中,我不能让IEnumerable工作,我应该得到一组数据,一个可能有一个项目(产品),另一个可能有很多(图像)。这些图像与productID绑定 在使用它之后,我现在

好了,在这里搜索了又搜索,谷歌和其他编程网站之后,是时候问我的第一个问题了

我有一个视图Index.cshtml,需要两个模型,所以我创建了一个ViewModel,ImageViewModel.cs,两个子模型ImageModel.cs和ProductModel.cs,还有一个控制器ImageController.cs

在我的一生中,我不能让IEnumerable工作,我应该得到一组数据,一个可能有一个项目(产品),另一个可能有很多(图像)。这些图像与productID绑定

在使用它之后,我现在看到了一个点,即在视图中,@Html.DisplayFor(modelItem=>item.imageTitle)抛出一个错误,说明imageTitle不存在

如果我还原分散在各处的众多IEnumerable项中的任何一项,我将继续得到传递的get ImageViewModel不是IEnumerable,并且视图希望它是IEnumerable

以下是完整的代码清单:

Index.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>
@model List<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.productName)
    </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>
ImageModel.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; }
}
}
ImageController.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);
    }
}
}
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();

    private JustBundleItContext db = new JustBundleItContext();

    //
    // GET: /JBI/Image/

    public ViewResult Index()
    {
        var model = from a in db.Product
                    join b in db.Image
                    on a.productID equals b.productID
                    select new ImageViewModel
                    {
                        Product = a,
                        Image = b
                    };
        return View(model.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);
    }
}
}
使用系统;
使用System.Collections.Generic;
使用系统数据;
使用System.Data.Entity;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
使用JustAdminIt.Areas.JBI.Models;
使用JustAdminIt.Areas.JBI.ViewModels;
使用JustAdminIt.Areas.JBI.DAL;
使用System.IO;
命名空间JustAdminIt.Areas.JBI.Controllers
{ 
公共类ImageController:控制器
{
私有ImageContext db=新ImageContext();
private ProductContext pb=new ProductContext();
//
//获取:/JBI/Image/
公共视图结果索引()
{
//尝试新事物
int-id=1;
IEnumerable image=db.image.TakeWhile(x=>x.productID==id);
IEnumerable product=pb.product.TakeWhile(x=>x.productID==id);
ImageViewModel piViewModel=新ImageViewModel(图像、产品);
返回视图(piViewModel);
//返回视图(db.Image.ToList());
}
//
//获取:/JBI/Image/Details/5
公共视图结果详细信息(int id)
{
Image=db.Image.Find(id);
返回视图(图像);
}
//
//获取:/JBI/Image/Create
公共操作结果创建()
{
返回视图();
} 
//
//POST:/JBI/Image/Create
//这只是填充DB值,但不处理文件上载
[HttpPost]
公共行动结果创建(图像)
{
if(ModelState.IsValid)
{
db.Image.Add(图像);
db.SaveChanges();
返回操作(“索引”);
}
返回视图(图像);
}
[HttpPost]
公共操作结果上载文件(HttpPostedFileBase文件数据)
{
//验证用户是否选择了一个文件
if(Filedata!=null&&Filedata.ContentLength>0)
{
//只提取fielname
var fileName=Path.GetFileName(Filedata.fileName);
//将文件存储在~/App_Data/uploads文件夹中
var path=path.Combine(Server.MapPath(“~/Content/images/product/”,文件名);
Filedata.SaveAs(路径);
}
//重定向回索引操作以再次显示表单
返回操作(“索引”);
}
//
//获取:/JBI/Image/Edit/5
公共操作结果编辑(int id)
{
Image=db.Image.Find(id);
返回视图(图像);
}
//
//POST:/JBI/Image/Edit/5
[HttpPost]
公共操作结果编辑(图像)
{
if(ModelState.IsValid)
{
db.Entry(image.State=EntityState.Modified;
db.SaveChanges();
返回操作(“索引”);
}
返回视图(图像);
}
//
//获取:/JBI/Image/Delete/5
公共操作结果删除(int id)
{
Image=db.Image.Find(id);
返回视图(图像);
}
//
//POST:/JBI/Image/Delete/5
[HttpPost,ActionName(“删除”)]
公共行动结果删除已确认(内部id)
{            
Image=db.Image.Find(id);
db.Image.Remove(图像);
db.SaveChanges();
返回操作(“索引”);
}
受保护的覆盖无效处置(布尔处置)
{
db.Dispose();
基地。处置(处置);
}
}
}
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;
    }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.ComponentModel.DataAnnotations;
使用系统组件模型;
使用System.Web.Mvc;
使用JustAdminIt.Areas.JBI.Models;
命名空间JustAdminIt.Areas.JBI.ViewModels
{
公共类ImageViewModel
{
公共IEnumerable映像{get;set;}
公共IEnumerable乘积{get;set;}
公共图像视图模型(IEnumerable图像、IEnumerable产品)
{
IEnumerable Image=图像;
i可数乘积=乘积;
}
}
}

在索引操作方法中,创建一个新的
ImageViewModel()
,然后将其传递给视图,但视图需要一个
IEnumerable

您要做的是创建一个ImageViewModel,然后将两个集合传递给它。这就像是用一个盒子装两组物品,而不是用一堆盒子装两个不同物品中的一个


你需要重新设计你的应用程序,以正确的方式生成项目。我不知道为什么这里有两种不同的数据上下文,但这会使事情变得非常复杂

问题可能是由于您使用了
TakeWhile

只要指定的条件为真,就从序列返回元素,然后跳过其余元素-

您可能希望使用
Where
there()

这只是其中的一部分。您正在传入一个包含这两个IEnumerable集的类。但是,您的视图接受该类的IEnumerable。你正确地通过,但没有正确地接收

@model JustAdminIt.Areas.JBI.ViewModels.ImageViewModel
然而,还有另一个问题
@foreach (var item in Model) {
@foreach (var item in Model.Product) {
public class MyDataContext : DbContext
{
    public DbSet<Bundle> Bundle { get; set; }
    public DbSet<Image> Image { get; set; }
    public DbSet<Product> Product { get; set; }
    public DbSet<Siteconfig> Siteconfig { get; set; }
}
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 Image Image { get; set; }
        public Product Product { get; set; }
    }
}
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();

    private JustBundleItContext db = new JustBundleItContext();

    //
    // GET: /JBI/Image/

    public ViewResult Index()
    {
        var model = from a in db.Product
                    join b in db.Image
                    on a.productID equals b.productID
                    select new ImageViewModel
                    {
                        Product = a,
                        Image = b
                    };
        return View(model.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);
    }
}
}
@model List<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.productName)
    </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>