skip to Main Content

jQuery Ajax call not hitting Controller Action in my application. I dont know happening with the code Jquery is not hitting. No error also showing I am running on my localhost, I don’t know if that is part of the problem, but outside of Sitecore, I have build ajax commands off of regular MVC applications with no issues.

This is my view

@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>

This is my controller

[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);
}

This is My function

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

2

Answers


  1. $.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');
            }
        });
    
    Login or Signup to reply.
  2. Please tyr this ajax and controller method

    **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');
            }
        });
    

    **Controller : **

    [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
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search