MVC - 无法加载资源:服务器响应状态为500(内部服务器错误)

问题描述:

我遇到了一些我为我的MVC 5项目编写的Ajax代码的问题,我试图为我的实现一个搜索栏表使用ajax,但我遇到了这个错误“无法加载资源:服务器响应状态为500(内部服务器错误)”。MVC - 无法加载资源:服务器响应状态为500(内部服务器错误)

这是我的局部视图代码,其中包括JavaScript的Ajax代码:

@model IEnumerable<ToDo.Models.Venue> 

@*Search Box*@ 
@using (Html.BeginForm()) 
{ 
    <p> 
     <input type="text" class="form-control" id="txtSearch"> 
     <span class="btn btn-sm btn-warning" id="btnCustomerInc" onclick="VenueSearch();">Search</span> 
    </p> 
} 

<table class="table"> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.VenueName) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.VenueType) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.VenueTown) 
     </th> 
     <th> 
      @*blank*@ 
     </th> 
    </tr> 

    @foreach (var item in Model) 
    { 
     <tr> 
      <td> 
       @Html.DisplayFor(modelItem => item.VenueName) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.VenueType) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.VenueTown) 
      </td> 
      <td> 
       @Html.ActionLink("Details", "Details", new { id = item.VenueID }, new { @class = "btn btn-primary" }) 
      </td> 
     </tr> 
    } 

</table> 

<script> 
    function VenueSearch() { 
     console.log("Venue Search function hit"); 

     var search = document.getElementById("txtSearch").value; 
     console.log(search); 

     $.ajax({ 
      type: "GET", 
      url: '@Url.Action("VenuesTablePartialView", "Venues")', 
      data: { searchString: search }, 
      success: function (data) { 

       $('#VenueTable').html(data); 
       $('#VenueTable').fadeIn("fast") 

      } 
     }); 
    } 
</script> 

这是我的局部视图控制器动作:

[ChildActionOnly] 
     public ActionResult VenuesTablePartialView(string searchString) 
     {  
      var venues = from v in db.Venues 
         select v; 

      //Search 
      if (!String.IsNullOrEmpty(searchString)) 
      { 
       venues = venues.Where(v => v.VenueName.ToUpper().Contains(searchString.ToUpper())); 
      } 


      return PartialView("_VenuesTable", venues.ToList()); 
     } 
    } 

This image shows the error and the console messages

+1

这将意味着控制器动作(或在管道中的一些过滤器或消息处理)抛出异常。抓住一个像Fiddler这样的工具,并根据更详细的错误信息查看它返回的内容,并从那里排除故障。 – jleach

+0

是的,我再看看控制器的动作,我意识到[ChildActionOnly]是我的问题的根源,我删除了这一行,现在按预期工作。 –

我能通过删除[ChildActionOnly]行来解决此问题,这引起了一些问题,因为这意味着此控制器操作只能由子r equests。

public ActionResult VenuesTablePartialView(string searchString) 
{  
    var venues = from v in db.Venues select v; 

    //Search 
    if (!String.IsNullOrEmpty(searchString)) 
    { 
     venues = venues.Where(v => v.VenueName.ToUpper().Contains(searchString.ToUpper())); 
    } 

    return PartialView("_VenuesTable", venues.ToList()); 
} 

将此代码添加到system.webServerweb.config文件

<validation validateIntegratedModeConfiguration="false" />