如何使用Ajax调用post方法

时间:2016-12-07 13:18:11

标签: c# ajax asp.net-mvc

我有两个ActionResults()方法[httpget]和[httppost]  我使用Ajax发布值,但不调用httpPostmethod 我使用的是MVC 4.0和C#,post方法没有调用。

[HttpGet]
public ActionResult Index()
{
    ViewBag.CrsCatgory = new SelectList(objIns.CoursCatgry(), "CC_ID", "Course_Catgory");

    var result = objstu.studentsearch();

    return View(result);
}

[HttpPost]
public ActionResult Index(string clas, string Demo, string CoursesName, string LocationName, string CourseCatagory, string fromdate, string todte)
{
    ViewBag.CrsCatgory = new SelectList(objIns.CoursCatgry(), "CC_ID", "Course_Catgory");

    var result = objstu.studentsearch(clas, Demo, CoursesName, LocationName, CourseCatagory, fromdate, todte);

    return View(result);
}

我正在编写Ajax代码

$(document).ready(function () {
    Loadlogos();
    LoadAllCoureses()

    $("#Searchtype").click(function () {
        debugger;
        var clas = $("#CheckClS").val();
        var Demo = $("#CheckDemo").val();
        var CoursesName = $("#Courselist").val();
        var LocationName = $("#autofillLocation").val();
        var CourseCatagory = $("#cat_id  option:selected").val();
        var fromdate = $("#frmdate").val();   
        var todate = $("#Todates").val();

        var url = "/Artists/Details?NestId=" + NestId;

        window.location.href = url;

        $.ajax({
            url: "Index",
            type: "POST",
            dataType: "json",
            data: "{ 'clas': '" + clas + "','Demo': '" + Demo + "','CoursesName': '" + CoursesName + "','LocationName':'" + LocationName + "','CourseCatagory': '" + CourseCatagory + "','fromdate': '" + fromdate + "','todte': '" + todate + "'}",
           contentType: "application/json; charset=utf-8",
           }
        });
});

0 个答案:

没有答案