0

I am calling jquery function on dropdown value change jquery method is ,

function MyFunction() {
    alert($('#DDlSurvey').val());
    $.ajax({
        url: "@Url.Action("GetSelectedQuestion", "ConductSurveyController")",
        data: { prefix: $('#DDlSurvey').val() },
    type: "GET",
    dataType: "json",
    success: function (data) {
        //  loadData(data);
        alert(data)
        alert("Success");
    },
    error: function () {
        alert("Failed! Please try again.");
    }
});
  //$('#YourLabelId').val('ReplaceWithThisValue');
}
</script>

Function I'm calling and I am getting dropdown value alert

Now, Function that I am calling is "GetSelectedQuestion" in controller "ConductSurveyController"

Method is like ,

[HttpPost]
public JsonResult GetSelectedQuestion(int prefix)
{
    List<SelectList> Questions=new List<SelectList>();

   //  Here "MyDatabaseEntities " is dbContext, which is created at time of model creation.
    SurveyAppEntities ObjectSur = new SurveyAppEntities();
       // Questions = ObjectSur.Surveys.Where(a => a.ID.Equals(prefix)).toToList();

I don't think this method is calling as I am getting error

"Failed! Please try again"

From my script.

Hopes for your suggestions

Thanks

 var e = from q in ObjectSur.Questions
         join b in ObjectSur.SurveyQuestions on q.ID equals b.QuestionID where b.SurveyID.Equals(prefix)
         select q ;
         return new JsonResult { Data = e, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
1
  • What http status code is being returned for that request? Check Network tab in Chrome Developer Tools. Commented Nov 28, 2018 at 12:17

2 Answers 2

1

I think you are using controller name straight forward. your ajax code be something like this.

var PostData= { prefix: $('#DDlSurvey').val() }
var ajaxOptions = {
        type: "GET",
        url: '@Url.Action("GetSelectedQuestion", "ConductSurvey")',//Actionname, ControllerName
        data: PostData,
        dataType: "json",
        success: function (result) {
            console.log(result);
        },
        error: function (result) {

        }
};
$.ajax(ajaxOptions);
Sign up to request clarification or add additional context in comments.

3 Comments

true man i just finished it with same changes you posted anyways thanks
Sorry i misunderstand issue is same as you said but i tried simple method in controller instead one i mentioned in post after making same changes still getting same error perhaps there is error in linq or given method ? how can i check linq
remove all code and just return simple string message from your MVC controller
1

Your method in your controller is decorated with HttpPost, while in your ajax you have specified the type of your request is get . You can either change your method to get like this:

[HttpGet]
public JsonResult GetSelectedQuestion(int prefix)
{
}

Or change your request type to post in your Ajax call:

$.ajax({
    url: "@Url.Action("GetSelectedQuestion", "ConductSurveyController")",
    data: { prefix: $('#DDlSurvey').val() },
    type: "Post",

Also the Controller is redundant in ConductSurveyController, you need to remove it and simply call it as ConductSurvey:

url: '@Url.Action("GetSelectedQuestion", "ConductSurvey")',

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.