如何在 ASP.net MVC 中使用 @Ajax.ActionLink() 获取数据



我想使用@Ajax.ActionLink()方法获取数据。 我尝试了以下方式
Create.chtml

 function UpdatePoDetails() {
    document.getElementById("poSearchbtn").href = "/MaterialReceivePO/SearchPO?searchID=" + document.getElementById('POID').value
}
@using (Ajax.BeginForm(new AjaxOptions
        {
            UpdateTargetId = "searchData",
            LoadingElementId = "loading"
        }
    ))
{
  <input id="POName" type="text" />

  @Ajax.ActionLink("Search", "SearchPO", null, new AjaxOptions
                                                        {
                                                            UpdateTargetId = "PoDetailsDiv",
                                                            HttpMethod = "GET" 

                                                        },
                                              new
                                              {
                                                  onclick = "UpdatePoDetails()" ,
                                                  id = "poSearchbtn"
                                              }
                            )

}
  @using (Ajax.BeginForm(new AjaxOptions
        {
            UpdateTargetId = "MainBody",
            LoadingElementId = "loading"
        }))
     {
         <div id="PoDetailsDiv">
          </div>
      }

控制器方法

 public ActionResult SearchPO(string searchID)
    {
        int id = int.Parse(searchID);
        List<PurchaseOrderDetailsModel> podetails = (
                                                         from c in po.GetPoListDetails(id)
                                                         select new PurchaseOrderDetailsModel()
                                                         {
                                                             PoDetailsID = c.PoDetailsID,
                                                             ItemID = c.ItemID.Value,
                                                             Quantity = c.Quantity,
                                                             UnitPrice = c.UnitPrice,
                                                             TotalPrice = c.TotalPrice,
                                                             DiscountPer = c.DiscountPer,
                                                             FinalPrice = c.FinalPrice,
                                                             CurrencyID = c.CurrencyID,
                                                             ProductID = c.ItemID.Value,
                                                             ProductName = c.ProductName,
                                                             ProductCode = c.ProductCode,
                                                             Description = c.Description
                                                         }
                                                    ).ToList();
        return View("SearchPO",podetails);
    }

SearchPO.chtml

@model IEnumerable<ERP_Web.Areas.Inventory.Models.PurchaseOrderDetailsModel>
<table class="grid-table">
    <tr>
        <th>
            Product Name
        </th>
        <th>
            Code
        </th>
        <th>
            Quantity
        </th>
        <th>
            Unit price
        </th>
        <th>
            Total
        </th>
        <th>
            Discount
        </th>
        <th>
            Final price
        </th>
        <th>
            Receive Quantity
        </th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.ProductName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ProductCode)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Quantity)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.UnitPrice)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.TotalPrice)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.DiscountPer)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FinalPrice)
            </td>
            <td>
                @Html.TextBox("reQuantity");
            </td>
        </tr>
    }
</table>

poblem 是当单击 Ajax 链接时,它会转到控制器。 控制器代码执行良好,但最后返回时不会调用 SearchPO 视图。我的代码中有什么问题或我缺少什么。有什么帮助吗??

@Ajax.ActionLink("Search", "SearchPO", null, 
new AjaxOptions{UpdateTargetId = "PoDetailsDiv",HttpMethod = "GET" },new {id = "poSearchbtn"})

我不明白为什么你需要这个点击="更新PoDetails()"

public ActionResult SearchPO(string searchID)
    {
    //Do your logic here
    //Build Mark-up of the result
    string GeneratedMarkUp = BuildMarkUpFOrtheResult();
    return Json(GeneratedMarkUp);
    }

最新更新