Jquery没有命中,也没有显示错误

zvms9eto  于 2023-04-29  发布在  jQuery
关注(0)|答案(2)|浏览(132)

jQuery AJAX 调用在我的应用程序中没有击中Controller Action。我不知道发生的代码Jquery是不是命中。没有错误也显示我在我的本地主机上运行,我不知道这是否是问题的一部分,但在Sitecore之外,我已经构建 AJAX 命令关闭常规MVC应用程序没有问题。
这是我的看法

@model BusinessEntity.BEModel.AssessmentDetailViewModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Assessement_Mapping</title>
    <script src="~/lib/jquery/dist/jquery.js"></script>
    
     <script type="text/javascript">

         $(document).ready(function () {

            $("#Assessment_Id").change(function (event) {

                    alert("You have Selected  :: " + $(this).val());
                    var item = $(this).val();

                $.ajax({
                    type: 'POST',
                    url: '@Url.Action("Assessement_Mapping_Data", "Detail")',
                    dataType: 'json',
                    data: { id: $("#Assessment_Id").val() },
                    success: function (x) {
                        $.each(x, function (i, y) {
                          $("#UserID").append('$('#IsActive').prop('checked', true);');
                        });
                    },
                    error: function (err) {
                        alert('Invalid operation, try again');
                    }
                });
            });

         })

    </script>

</head>

<body>

<h4>AssessmentDetailViewModel</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Assessement_Mapping">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
           @* <div class="form-group">
                <label asp-for="Assessment_Id" class="control-label"></label>
                <input asp-for="Assessment_Id" class="form-control" />
                <span asp-validation-for="Assessment_Id" class="text-danger"></span>
            </div>*@
           
            <div class="form-group">
                <label asp-for="Assessment_Name" class="control-label"></label>
                <select asp-for="Assessment_Id" asp-items="@(new SelectList(Model.Assinfo,"Assessment_Id","Assessment_Name"))"></select>
                <span asp-validation-for="Assessment_Name" class="text-danger"></span>
            </div>
          
           @* <div class="form-group">
                <label asp-for="IsActive" class="control-label"></label>
                <input asp-for="IsActive" class="form-control" />
                <span asp-validation-for="IsActive" class="text-danger"></span>
            </div>*@
           @*
            <div class="form-group">
                <label asp-for="UserID" class="control-label"></label>
                <input asp-for="UserID" class="form-control" />
                <span asp-validation-for="UserID" class="text-danger"></span>
            </div>*@
            <div class="form-group">
                <label asp-for="UserName" class="control-label"></label>
              @*  <input asp-for="UserName" class="form-control" />*@
                  
                    @for (var i = 0; i < Model.Userinfo.Count; i++)
                    {
                        <input type="checkbox" asp-for="@Model.Userinfo[i].IsActive" />
                        <label asp-for="@Model.Userinfo[i]">@Model.Userinfo[i].UserName</label>
                        <input type="hidden" asp-for="@Model.Userinfo[i].UserID" />
                        <input type="hidden" asp-for="@Model.Userinfo[i].UserName" />
                    }
                <span asp-validation-for="UserName" class="text-danger"></span>
            </div>
           @* <div class="form-group">
                <label asp-for="Name" class="control-label"></label>
                <input asp-for="Name" class="form-control" />
                <span asp-validation-for="Name" class="text-danger"></span>
            </div>*@
           
           
          
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

</body>
</html>

这是我的控制器

[HttpGet]
public JsonResult Assessement_Mapping_Data(int id)
{
     List<AssessmentMapping> lst1 = obj.getallusers(id);
            AssessmentDetailViewModel productInfo = new AssessmentDetailViewModel();

            productInfo.Assmappinginfo = lst1;
            StringBuilder sd = new StringBuilder();
            foreach (AssessmentMapping item in lst1)
            {
                if (item.IsActive)
                {
                    sd.Append(sd.ToString());
                }
            }
            //ViewBag.Selecteduser = sd;

            return Json(productInfo);
}

这是我的功能

public List<AssessmentMapping> getallusers(int id)
{
    List<AssessmentMapping> lstobj = new List<AssessmentMapping>();
    lstobj=_context.AssessmentMappingData.Where(m=>m.Assessment_Id==id).ToList();
    return lstobj;
}
dy2hfwbg

dy2hfwbg1#

请尝试此 AJAX 和控制器方法
** AJAX :**

$.ajax({
        type: 'GET',  //Change POST to GET
        url: '@Url.Action("Assessement_Mapping_Data")',
        dataType: 'json',
        data: { id: $("#Assessment_Id").val() },
         success: function (x) {
             alert("hi");
        $.each(x, function (i, y) {
                alert("hi");
                $("#UserID").append('< option value = "' + y.Value + '" > ' + y.Text + ' < /option>');
        });
        },
        error: function (err) {
            alert('Invalid operation, try again');
        }
    });

控制器:

[HttpGet]
    public JsonResult Assessement_Mapping_Data(int id)
    {
        List<AssessmentMapping> lst1 = obj.getallusers(id);
        AssessmentDetailViewModel productInfo = new AssessmentDetailViewModel();

        productInfo.Assmappinginfo = lst1;
        return Json(productInfo, JsonRequestBehavior.AllowGet); //add , JsonRequestBehavior.AllowGet
    }
irlmq6kh

irlmq6kh2#

$.ajax({
        type: 'POST',
                url: '@Url.Action("getallusers")',
        dataType: 'json',
                data: { id: $("#Assessment_Id").val() },
                **alert("hi");Remove this line **

        success: function (x) {
            $.each(x, function (i, y) {
                alert("hi");
                        $("#UserID").append('
                    < option value = "' + y.Value + '" > ' + y.Text + ' < 
/option>');
        });
        },
        error: function (err) {
            alert('Invalid operation, try again');
        }
    });

相关问题