C# 当我的表不可枚举时,如何显示SQL数据库中的列表?我可以使用细节视图,但不能使用索引视图
我很难让索引视图显示我从SQL数据库获得的值。C# 当我的表不可枚举时,如何显示SQL数据库中的列表?我可以使用细节视图,但不能使用索引视图,c#,asp.net-mvc,entity-framework,razor,C#,Asp.net Mvc,Entity Framework,Razor,我很难让索引视图显示我从SQL数据库获得的值。 主要的问题是我不能使用@foreach(模型中的var项){…,因为我的表不是创建为可枚举的(我想)。我遇到错误消息System.NullReferenceException:“对象引用未设置为对象的实例”。指向foreach语句表达式中的Model 我想知道我是否需要将我的表设置为可枚举列表,然后显示每个项目。或者我是否缺少有关索引视图的内容。或者我可能需要通过索引返回视图()传递一些内容。 以下是图像模型: namespace Uploadim
主要的问题是我不能使用
@foreach(模型中的var项){…
,因为我的表不是创建为可枚举的(我想)。我遇到错误消息System.NullReferenceException:“对象引用未设置为对象的实例”。
指向foreach
语句表达式中的Model
我想知道我是否需要将我的表设置为可枚举列表,然后显示每个项目。或者我是否缺少有关索引视图的内容。或者我可能需要通过索引返回视图()传递一些内容。
以下是图像模型:
namespace Uploadimage.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web;
public partial class Image
{
public int Id { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
public string City { get; set; }
public string Province { get; set; }
public string Phone { get; set; }
[Required]
[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}",
ErrorMessage = "Email doesn't look like a valid email address.")]
public string Email { get; set; }
[Compare("Email", ErrorMessage = "Emails do not match.")]
public string ConfirmEmail { get; set; }
[DisplayName("Upload File")]
public string ImagePath { get; set; }
public HttpPostedFileBase ImageFile { get; set; }
}
}
这是控制器:
public class ImageController : Controller
{
[HttpGet]
public ActionResult Add()
{
return View();
}
[HttpPost]
public ActionResult Add(Image imageModel)
{
string fileName = Path.GetFileNameWithoutExtension(imageModel.ImageFile.FileName);
string extension = Path.GetExtension(imageModel.ImageFile.FileName);
fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;
imageModel.ImagePath = "~/Image/" + fileName;
fileName = Path.Combine(Server.MapPath("~/Image/"), fileName);
imageModel.ImageFile.SaveAs(fileName);
using(LoginDBEntities db = new LoginDBEntities())
{
db.Images.Add(imageModel);
db.SaveChanges();
}
ModelState.Clear();
return View();
}
[HttpGet]
public ActionResult View(int id)
{
Image imageModel = new Image();
using (LoginDBEntities db = new LoginDBEntities())
{
imageModel = db.Images.Where(x => x.Id == id).FirstOrDefault();
}
return View(imageModel);
}
public ActionResult Index()
{
return View();
}
}
最后,我的索引视图:
@model IEnumerable<Uploadimage.Models.Image>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
<th>
@Html.DisplayNameFor(model => model.City)
</th>
<th>
@Html.DisplayNameFor(model => model.Province)
</th>
<th>
@Html.DisplayNameFor(model => model.Phone)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th>
@Html.DisplayNameFor(model => model.ImagePath)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.City)
</td>
<td>
@Html.DisplayFor(modelItem => item.Province)
</td>
<td>
@Html.DisplayFor(modelItem => item.Phone)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.ImagePath)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
@model IEnumerable
@{
ViewBag.Title=“Index”;
}
指数
@ActionLink(“新建”、“创建”)
@DisplayNameFor(model=>model.FirstName)
@DisplayNameFor(model=>model.LastName)
@DisplayNameFor(model=>model.City)
@DisplayNameFor(model=>model.Province)
@DisplayNameFor(model=>model.Phone)
@DisplayNameFor(model=>model.Email)
@DisplayNameFor(model=>model.ImagePath)
@foreach(模型中的var项目){
@DisplayFor(modelItem=>item.FirstName)
@DisplayFor(modelItem=>item.LastName)
@DisplayFor(modeleItem=>item.City)
@DisplayFor(modelItem=>item.Province)
@DisplayFor(modeleItem=>item.Phone)
@DisplayFor(modelItem=>item.Email)
@DisplayFor(modelItem=>item.ImagePath)
@ActionLink(“编辑”,“编辑”,新的{id=item.id})|
@ActionLink(“详细信息”,“详细信息”,新的{id=item.id})|
@ActionLink(“删除”,“删除”,新的{id=item.id})
}
我已尝试彻底查找此信息,但发现了不适用于我的内容。或者我不知道要查找什么。您的索引方法中没有任何可以获取实体列表的内容。 控制器中的索引方法应类似于:
public ActionResult Index()
{
var model = db.Images.ToList();
return View(model);
}
两件事:
您的索引控制器操作将需要加载图像以传递给视图。至少要将一组图像作为视图的“模型”提供服务:
然后,在视图中,您需要检查是否实际得到任何结果,然后从第一个结果中提取标签(如果有),或者如果没有图像,则显示适当的消息:
<!-- ... -->
@if (Model.Any())
{
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.ElementAt(0).FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.ElementAt(0).LastName)
</th>
...
</tr>
@for (int count = 0; count < Model.Count; count++) {
<tr>
<td>
@Html.DisplayFor(model => model.ElementAt(count).FirstName)
</td>
<td>
@Html.DisplayFor(model => model.ElementAt(count).LastName)
</td>
...
<td>
@Html.ActionLink("Edit", "Edit", new { id=@Model.ElementAt(count).Id }) |
@Html.ActionLink("Details", "Details", new { id=@Model.ElementAt(count).Id }) |
@Html.ActionLink("Delete", "Delete", new { id=@Model.ElementAt(count).Id })
</td>
</tr>
}
</table>
}
else
{
<p> No Images. </p>
}
@if(Model.Any())
{
@DisplayNameFor(model=>model.ElementAt(0.FirstName)
@DisplayNameFor(model=>model.ElementAt(0.LastName)
...
@for(int count=0;countmodel.ElementAt(count.FirstName)
@DisplayFor(model=>model.ElementAt(count.LastName)
...
@ActionLink(“编辑”,“编辑”,新的{id=@Model.ElementAt(count.id})|
@ActionLink(“Details”,“Details”,new{id=@Model.ElementAt(count.id})|
@ActionLink(“Delete”,“Delete”,new{id=@Model.ElementAt(count.id})
}
}
其他的
{
没有图像
}
上面的内容是从内存中写出来的,因此可能会有一些语法问题,但它应该为您指明正确的方向
通常,在处理搜索结果等集合时,我会为结果页面定义一个视图模型类,该页面本身包含结果集合。该页面的模型将成为包装视图模型,该模型可以包含页面的详细信息(例如当前搜索条件、搜索的查找值等)以及结果集合。(通常是支持分页的页面列表)
即
[可序列化]
公共类ImageIndexViewModel
{
公共字符串名称搜索字符串{get;set;}
公共ICollection结果{get;set;}=new List();
}
其中ImageViewModel是一个可序列化的POCO视图模型,仅表示视图将显示的图像的详细信息。视图通常不需要关于数据行的所有信息,如果数据行具有导航属性和我们不需要显示的额外字段,序列化实体将导致延迟加载调用或简单地发送正在删除大量不需要的额外数据。是的,从原始数据复制粘贴错误,已更正。:)
<!-- ... -->
@if (Model.Any())
{
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.ElementAt(0).FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.ElementAt(0).LastName)
</th>
...
</tr>
@for (int count = 0; count < Model.Count; count++) {
<tr>
<td>
@Html.DisplayFor(model => model.ElementAt(count).FirstName)
</td>
<td>
@Html.DisplayFor(model => model.ElementAt(count).LastName)
</td>
...
<td>
@Html.ActionLink("Edit", "Edit", new { id=@Model.ElementAt(count).Id }) |
@Html.ActionLink("Details", "Details", new { id=@Model.ElementAt(count).Id }) |
@Html.ActionLink("Delete", "Delete", new { id=@Model.ElementAt(count).Id })
</td>
</tr>
}
</table>
}
else
{
<p> No Images. </p>
}
[Serializable]
public class ImageIndexViewModel
{
public string NameSearchString { get; set; }
public ICollection<ImageViewModel> Results { get; set; } = new List<ImageViewModel>();
}