尝试使用 Ajax 调用控制器方法 MVC 获取数据我的代码已附
Trying to Get Data using Ajax call to Controller method MVC My code Attached
我在下拉值更改时调用 jquery 函数 jquery 方法是,
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>
我正在调用的函数,我收到了下拉值提醒
现在,我正在调用的函数是控制器“ConductSurveyController
”中的“GetSelectedQuestion
”
方法就像,
[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();
我不认为这个方法正在调用,因为我收到错误
"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 };
}
您控制器中的方法用 HttpPost
装饰,而在您的 ajax 中您指定的请求类型是 get
。你可以改变你的方法来得到这样的:
[HttpGet]
public JsonResult GetSelectedQuestion(int prefix)
{
}
或者在 Ajax 调用中将您的请求类型更改为 post:
$.ajax({
url: "@Url.Action("GetSelectedQuestion", "ConductSurveyController")",
data: { prefix: $('#DDlSurvey').val() },
type: "Post",
此外 Controller
在 ConductSurveyController
中是多余的,您需要将其删除并简单地称其为 ConductSurvey
:
url: '@Url.Action("GetSelectedQuestion", "ConductSurvey")',
我认为您直接使用了控制器名称。你的 ajax 代码应该是这样的。
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);
我在下拉值更改时调用 jquery 函数 jquery 方法是,
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>
我正在调用的函数,我收到了下拉值提醒
现在,我正在调用的函数是控制器“ConductSurveyController
”中的“GetSelectedQuestion
”
方法就像,
[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();
我不认为这个方法正在调用,因为我收到错误
"
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 };
}
您控制器中的方法用 HttpPost
装饰,而在您的 ajax 中您指定的请求类型是 get
。你可以改变你的方法来得到这样的:
[HttpGet]
public JsonResult GetSelectedQuestion(int prefix)
{
}
或者在 Ajax 调用中将您的请求类型更改为 post:
$.ajax({
url: "@Url.Action("GetSelectedQuestion", "ConductSurveyController")",
data: { prefix: $('#DDlSurvey').val() },
type: "Post",
此外 Controller
在 ConductSurveyController
中是多余的,您需要将其删除并简单地称其为 ConductSurvey
:
url: '@Url.Action("GetSelectedQuestion", "ConductSurvey")',
我认为您直接使用了控制器名称。你的 ajax 代码应该是这样的。
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);