【问题标题】:System.Data.EntityCommandExecutionException IssueSystem.Data.EntityCommandExecutionException 问题
【发布时间】:2015-08-01 22:49:45
【问题描述】:

dbo.Images - 表格

http://imgur.com/9YVNfZz

图像.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; }
        }
    }

ImageController.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);
}

Search.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 上,以便从包含ExtrasHomeCategory 中过滤987654331@表。尽管该页面应根据单击的 Category 按钮显示图像,但我遇到了如下错误。

任何想法如何解决它? 感谢您的帮助,并在此先感谢您。

【问题讨论】:

    标签: c# html asp.net asp.net-mvc razor


    【解决方案1】:

    您可以将所选类别作为查询字符串参数发送到您的控制器,然后在那里过滤您的列表。请注意,ActionLinks 将针对您的 HttpGetaction 方法。将值替换为您拥有的类别。此外,这种方法不需要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);
      }
    

    【讨论】:

    • 感谢您的意见。我已经按照说明实现了代码,但是一旦按下按钮就会显示错误并且应用程序崩溃。 Search.cshtml 已加载-imgur.com/ToMK2vy 按钮单击-imgur.com/TYYTI6S 代码-imgur.com/4jhPoRc
    • 我相信所有的动作链接都应该是这样的:@Html.ActionLink("Search", "Image",.....);这样所有的按钮都会在控制器中触发相同的动作。
    • 将“类别”类型从 TEXT 切换到 NAVCHAR(MAX) 但现在出现了另一个问题 - i.imgur.com/tEEvLLt.jpg
    • 如果您使用的是Layout.cshtml 页面和_ViewStart.cshtml,您可以删除Viewbag.Title = "Search"。至于您的例外,它与您的数据类型无关。它告诉您您正在尝试从其中没有值的内容中读取。将您的 foreach 包装在 'if(Model != null){}' 中并检查获取值的控制器
    【解决方案2】:

    试试这样的。在这种方法中,用户可以从下拉列表中选择一个类别并单击搜索按钮。

    控制器

        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>
    

    【讨论】:

    • 感谢您的宝贵时间,但尝试了一下 (imgur.com/tvXbIOB),似乎在单击搜索按钮时没有任何反应。知道为什么吗?
    • 您是否从 NuGet 添加了 jquery.unobtrusive-ajax.js?
    • 如果您不介意或最好保留浏览器历史记录,那么@brroshan 回答是一个好方法。我的方法不会向浏览器添加回栈。
    • jquery.unobtrusive-ajax.js 已设置 =/ .. 尝试实现 @brroshan 代码但应用程序崩溃了。
    • 非常感谢您的帮助!
    猜你喜欢
    • 2011-08-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 2016-01-22
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多