尝试使用对控制器方法MVC的Ajax调用获取数据 [英] Trying to Get Data using Ajax call to Controller method MVC My code Attached

查看:152
本文介绍了尝试使用对控制器方法MVC的Ajax调用获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下拉列表中更改值时调用jquery函数,jquery方法是,

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

现在,我正在调用的函数是控制器"ConductSurveyController"中的"GetSelectedQuestion"

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

方法就像,

[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"

从我的脚本开始.

希望获得您的建议

谢谢

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

推荐答案

我认为您正在直接使用控制器名称.您的Ajax代码就是这样.

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

这篇关于尝试使用对控制器方法MVC的Ajax调用获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆