尝试使用对控制器方法MVC的Ajax调用获取数据附加了我的代码

时间:2018-11-28 12:13:08

标签: c# jquery json asp.net-mvc asp.net-ajax

我在下拉值更改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>

我正在调用的函数,并且正在接收下拉值警报

现在,我正在调用的函数是控制器“ GetSelectedQuestion”中的“ 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();

由于出现错误,我不认为此方法正在调用

  

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

2 个答案:

答案 0 :(得分:1)

您的控制器中的方法用select convert(date, stuff(datestr, 1, charindex(',', datestr) + 1, '') ) from (values ('Wednesday, November 21, 2018')) v(datestr) 装饰,而在ajax中,您将请求的类型指定为HttpPost。您可以更改方法,使其像这样:

get

或更改您的请求类型以在您的Ajax调用中发布:

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

此外,$.ajax({ url: "@Url.Action("GetSelectedQuestion", "ConductSurveyController")", data: { prefix: $('#DDlSurvey').val() }, type: "Post", Controller中是多余的,您需要将其删除并简单地将其称为ConductSurveyController

ConductSurvey

答案 1 :(得分:1)

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

Sub findValue()

Dim xlRange As Range
Dim xlCell As Range
Dim xlSheet As Worksheet
Dim valueToFind

For Each cell In Range("h2:h8")
cell.Select
cell = ActiveCell
MsgBox (cell)

valueToFind = ActiveCell
Set xlSheet = ActiveWorkbook.Worksheets("DATA")
Set xlRange = xlSheet.Range("A1:A13")

For Each xlCell In xlRange


    If xlCell.Value = valueToFind Then
        MsgBox (xlCell.Offset(0, 1).Value)
  valueToFind.Offset(0, 2).Value = xlCell.Offset(0, 1).Value

    End If
Next xlCell