ASP.Net MVC中的GET方法没有从Javascript按钮点击调用

时间:2014-10-04 20:07:10

标签: javascript c# jquery asp.net-mvc

我是ASP.Net MVC的新手。我有一个按钮,然后单击我需要返回JSON对象的按钮。单击按钮应进行ajax调用以调用get方法。我的get方法根本没有调用,而是调用了我的索引方法。

控制器:

 public ActionResult PageLoad()
    {
        return View("PackBOMQuotePreferred");
    }

     [HttpGet]
    public ActionResult RetreivePackData()
    {

        IEnumerable<MPMPackInfo> oListOfPacks = null;
        try
        {
           oListOfPacks = _service.GetListOfPacks();

        }
        catch (Exception ex)
        {
            _errorLog.Error(ex);
        }
        return Json(oListOfPacks, JsonRequestBehavior.AllowGet);
    }

.cshtml

 @{
    ViewBag.Title = "PackBOMQuotePreferred";
}

<h2>PackBOMQuotePreferred</h2>


<!DOCTYPE html>

<meta name="viewport" content="width=device-width" />
<script src="~/Scripts/jquery-1.9.1.min.js" type="text/javascript"></script>

 <div id="errorMsgBoard" class="error">
 </div>
<div>
    <input type="button" class ="btn btn-primary start"  id ="btnRetreivePackData" value="Retreive Pack Data" />
    <input type="button" class ="btn btn-primary start" id ="btnRetreiveBOMData" value="Retrieve BOM Data" />
    <input type="button" class ="btn btn-primary start" id ="btnRetreiveQUoteData" value="Retrieve Quote Data" />
    <input type="button" class ="btn btn-primary start" id ="btnRetreivePreferredData" value="Retrieve Preferred Data" />
</div>




<script type="text/javascript" lang="javascript">
    $("#btnRetreivePackData").on("click", function ()
    {

        $.ajax({
            type: 'get',
            Url: 'PackBOMQuotePreferred/RetreivePackData',
            datType:'json',
            cache: false,
            data: {},
            contentType: 'application/json; charset=utf-8',
            Success: function (response, textStatus, jqXHR) {
                alert(response);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert('Error - ' + errorThrown);
            }
        });
    });

</script>

如果我看一下fiddler,它会显示GET / PackBOMQuotePreferred / PageLoad?_ = 1412451506195 HTTP / 1.1,表示RetreivePackData没有被调用。我没有在Chrome控制台中看到任何错误。

不确定我做错了什么。

1 个答案:

答案 0 :(得分:0)

修改您的网址。它应该是

Url: '/PackBOMQuotePreferred/RetreivePackData/',

您还错误datatypedattype