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

时间:2012-08-14 03:30:31

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

我希望使用@ 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>

这个问题是当它点击Ajax链接时它会转到Controller。控制器代码执行良好但最后在返回时不调用SearchPO视图。我的代码中有什么问题或者我错过了什么。任何帮助?

1 个答案:

答案 0 :(得分:0)

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

我不明白为什么你需要这个 onclick =“UpdatePoDetails()”

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