通过Ajax调用一个动作

时间:2016-06-22 05:56:25

标签: jquery ajax model-view-controller httprequest

我在一个asp.net mvc项目上工作,我想通过选择另一个下拉列表来刷新下拉列表。为此使用ajax,但是当运行请求转到错误方法而不是运行成功方法时。

我的剧本

<script src="~/Scripts/jquery-2.0.0.min.js"></script>
<script>

function FillSubject() {

    var GroupServicesId = $('#GroupServices_ID').val();
    $.ajax({
        url: "/Advertisements/FillSubject",
        type: "GET",
        data: { 'GroupServiceID': GroupServicesId },
        dataType:"json",
        success: function (Subjects) {
            $("#Subject_ID").html("");
            $.each(Subjects, function (i, subject) {
                $("#Subject_ID").append($('<option></option>').val(subject.ID).html(subject.strSubjectName));
            });
        },
        error: function (e) {
            alert(this.url);
        }
    });
}
</script>`

我在控制器中的行动

    public JsonResult FillSubject(string GroupServiceID)
    {
        //string GSID = GroupServiceID.Substring(1, (GroupServiceID.Length)-2);
        var Subjects = db.Subjects.Where(c => c.GroupServicesID.ToString() == GroupServiceID);
        var SubjectList = Subjects.Select(sl => new SubjectList { ID = sl.ID, GroupServiceID = sl.GroupServicesID, strSubjectName = sl.strSubjectName });
        return Json(SubjectList,JsonRequestBehavior.AllowGet);
    }

这是我在.cshtml / razor代码中的下拉列表

<div class="form-group">
            @Html.LabelFor(model => model.GroupServices_ID, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.GroupServices_ID, new SelectList(GroupServicesList, "ID", "strGroupServiceName"), new { @class = "selectpicker", @onchange = "FillSubject()" })
                @Html.ValidationMessageFor(model => model.GroupServices_ID, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Subject_ID, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.Subject_ID, new SelectList(SubjectsList, "ID", "strSubjectName"), new { @class = "selectpicker" })
                @Html.ValidationMessageFor(model => model.Subject_ID, "", new { @class = "text-danger" })
            </div>
        </div>

2016年1月22日更新

我更改了我的控制器并在Json result中提供了ajax但未更新我的第二个下拉列表 我的结果 [Object { ID="ffb1d85a-a822-41e1-8859-24db2b5bc72e", GroupServiceID="0cc2cdf6-b9d3-4274-9cc1-31daf5814458", strSubjectName="برنامه نویسی"}, Object { ID="1f65f038-0111-479a-bdf3-d43eb772af9d", GroupServiceID="0cc2cdf6-b9d3-4274-9cc1-31daf5814458", strSubjectName="آموزش زبان خارجه"}]

1 个答案:

答案 0 :(得分:0)

请试一试,我不完全确定它是否有效.. !!
但我们试试..

在您的AJAX请求中将type: "GET",更改为type: "POST",

public JsonResult FillSubject(string GroupServiceID)

[HttpPost]
public ActionResult FillSubject(string GroupServiceID)
{
   // do stuff
   return Json(SubjectList,JsonRequestBehavior.AllowGet);

  //Updated Code
   return Json(SubjectList);
}
  

更新

首先,尝试使用这样的简单代码。

public ActionResult FillSubject(string GroupServiceID)
{
   return Json(true);
}

并通过将调试器放入这样的成功方法来检查,

function FillSubject() {

var GroupServicesId = $('#GroupServices_ID').val();
$.ajax({
    url: "/Advertisements/FillSubject?GroupServiceID=" + "test",
    type: "GET",
    success: function (result) {
        debugger;
        alert(result);
    },
    error: function (e) {
        alert(this.url);
    }
});
}