@ Ajax.ActionLink没有将数据发送到控制器ASP.Net MVC?

时间:2015-12-17 07:58:21

标签: jquery ajax asp.net-mvc razor asp.net-mvc-5

我正在使用ASP.Net MVC 5Razor Engine 我想实现一个页面来使用ajax显示产品。

这是我的代码:

HomeController:

public ActionResult GoodDetails(int id)
    {
        Models.NishtmanDBEntities db = new NishtmanDBEntities();
        BlGoodDetail blGoodDetail = new BlGoodDetail();
        HomeVM vm = new HomeVM();

        var goodDetails = db.GoodDetails.Where(p => p.FKSubGoods == id).ToList();
        vm.GoodDetails = goodDetails;
        return View(vm);

    }
public PartialViewResult GoodDetailsAjax(int id)
    {
        HomeVM vm = new HomeVM();
        var GoodDetailsAjax = db.GoodDetails.Where(p => p.DetailsGoodID == id).ToList();
        vm.GoodDetails = GoodDetailsAjax;
        return PartialView("_GoodDetailsAjax", vm);
    }

GoodDetails.cshtml:

@model NP1.ViewModels.HomeVM
@section scripts{
  <script src="~/Scripts/jquery-2.1.4.min.js"></script>
  <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
  }

<div style="width: 100%; background-color: white;">
    @foreach (var item1 in Model.GoodDetails)
       {
     <a href="@Url.Action("GoodDetailsAjax", "Home", new { id = @item1.DetailsGoodID })">
     @Ajax.ActionLink(" ", "GoodDetailsAjax", new AjaxOptions()
          {
              HttpMethod = "GET",
              UpdateTargetId = "DivAjax",
              InsertionMode = InsertionMode.Replace
          }
          )


    <input type="image" class="img-responsive" style="width:120px; height:120px;" src="@Url.Content(item1.DetailsSmallImage1.ToString())">

</a>
 }
 </div>
 <div id="DivAjax">

</div>

_GoodDetailsAjax.cshtml:

@model NP1.ViewModels.HomeVM
<div>
 @foreach (var item in Model.GoodDetails)
 {
    <img class="img-responsive" src="@Url.Content(item.DetailsImage1.ToString())" style="width:400px;height:320px;">
    <img class="img-responsive" src="@Url.Content(item.DetailsImage2.ToString())" style="width:400px;height:320px;">
    <img class="img-responsive" src="@Url.Content(item.DetailsImage3.ToString())" style="width:400px;height:320px;">
    <img class="img-responsive" src="@Url.Content(item.DetailsImage3.ToString())" style="width:400px;height:320px;">

}

我的疑问是:没有错误,为什么它不起作用?当我点击GoodDetails中的图片时,没有任何事情发生。

1 个答案:

答案 0 :(得分:0)

您的代码应为

@Ajax.ActionLink(" ", "GoodDetailsAjax", new { id = @item1.DetailsGoodID }, new AjaxOptions()
{
HttpMethod = "GET",
UpdateTargetId = "DivAjax",
InsertionMode = InsertionMode.Replace
}
)

您没有将new { id = @item1.DetailsGoodID }传递给ActionLink。这是你在Controller Action中没有得到id的问题。