System.Data.EntityCommandExecutionException Issue
本文关键字:Issue EntityCommandExecutionException Data System | 更新日期: 2023-09-27 18:05:50
德博。图片 - 表格
https://i.stack.imgur.com/zRI9Z.jpg
图片.cs
public class Image
{
public int ID { get; set; }
public string ImagePath { get; set; }
public string Category { get; set; }
}
public class ImageDBContext : DbContext
{
public DbSet<Image> Images { get; set; }
}
}
图像控制器.cs
[HttpGet]
public ActionResult Search(string category)
{
var myList = db.Images.ToList();
if (!string.IsNullOrEmpty(category))
{
myList = db.Images.Where(s => s.Category == category).ToList();
}
return View(myList);
}
搜索.cshtml
@model IEnumerable<MvcMovie.Models.Image>
@{
ViewBag.Title = "Search";
}
<button>@Html.ActionLink("Extras", "Search", new { category = "Extras" })</button>
<button>@Html.ActionLink("Home", "Search", new { category = "Home" })</button>
<div style="width:800px; margin:200px auto; text-align:center;">
<ul class="images">
@foreach (var item in Model)
{
var imagePath = @item.ImagePath;
imagePath = "/Images/" + imagePath;
<li class="image-item" style="background: url(@imagePath) no-repeat;"></li>
}
</ul>
关于上面的代码,应用程序当前将显示在dbo.Images
中找到的所有图像到具有两个按钮的search.cshtml
上,以便从dbo.Images
表中保存Extras
或Home
的Category
中过滤。尽管页面应根据单击的按钮显示图像Category
但我遇到了以下错误。
- 按钮已单击-https://i.stack.imgur.com/sYSxm.png
有什么想法可以解决吗?感谢您的帮助和感谢。
您可以将所选类别作为查询字符串参数发送到控制器,然后在其中过滤列表。请注意,操作链接将针对HttpGet
操作方法。将值替换为您拥有的类别。此外,此方法不需要data
属性。
<button>@Html.ActionLink("Category1", "Search", new { category = "Category1" })</button>
<button>@Html.ActionLink("Category2", "Search", new { category = "Category2" })</button>
<button>@Html.ActionLink("Category3", "Search", new { category = "Category3" })</button>
<ul class="images">
@foreach (var item in Model)
{
var imagePath = @item.ImagePath;
imagePath = "/Images/" + imagePath;
<li class="image-item" style="background: url(@imagePath) no-repeat;"></li>
}
</ul>
控制器:
[HttpGet]
public ActionResult Search(string category) // <-- selected category
{
var myList = db.Images.ToList();
if(!string.IsNullOrEmpty(category)){
myList = db.Images.Where(s => s.Category == category).ToList();
}
return View(myList);
}
试试这样的事情。在这种方法中,用户可以从下拉列表中选择一个类别,然后单击搜索按钮。
控制器
private ImageDBContext db = new ImageDBContext ();
[HttpGet]
public ActionResult Index()
{
var myList = db.Images.ToList();
return View(myList);
}
[HttpPost]
public ActionResult Index(string category)
{
var myList = db.Images.ToList();
if (string.IsNullOrEmpty(category) || category == "All")
return PartialView("_SearchResult", myList);
if (!string.IsNullOrEmpty(category))
myList = myList.Where(m => m.Category == category).ToList();
return PartialView("_SearchResult", myList);
}
部分视图:_SearchResult
@model IEnumerable<MvcMovie.Models.Image>
<ul class="images">
@foreach (var item in Model)
{
var imagePath = @item.ImagePath;
imagePath = "/Images/" + imagePath;
<li class="image-item" data-filter="Fashion" data-filter- type="@item.Category.ToLower()" style="background: url(@imagePath) no-repeat; height: 200px;">@item.Category</li>
}
</ul>
视图
@model IEnumerable<MvcMovie.Models.Image>
<script src="@Url.Content("~/Scripts/jquery-1.10.2.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
@using (Ajax.BeginForm("Index", "Search", new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "result" }))
{
<select name="category">
<option>All</option>
<option>Home</option>
<option>Extras</option>
</select>
<input type="submit" value="Search" />
}
<div style="width: 800px; margin: 200px auto; text-align: center;">
<div id="result">
@Html.Partial("_SearchResult", Model)
</div>
</div>