【问题标题】:how to get data using @Ajax.ActionLink() in ASP.net MVC如何在 ASP.net MVC 中使用 @Ajax.ActionLink() 获取数据
【发布时间】:2012-08-14 03:30:30
【问题描述】:

我想使用@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 链接时,它会转到控制器。控制器代码执行良好,但最后返回时不调用 SearchPO 视图。我的代码有什么问题或我缺少什么。有什么帮助吗??

【问题讨论】:

  • 您能在 FireBug 中看到一些错误吗?
  • 你想把数据放到同一个页面,或者你想用结果集重定向到不同的页面?

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


【解决方案1】:
@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);
    }

【讨论】:

    猜你喜欢
    • 2021-08-10
    • 2016-10-04
    • 1970-01-01
    • 1970-01-01
    • 2014-08-01
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多