Asp.net mvc 4 在IpagedListPager Mvc上使用Ajax调用HttpGet方法

Asp.net mvc 4 在IpagedListPager Mvc上使用Ajax调用HttpGet方法,asp.net-mvc-4,asp.net-ajax,Asp.net Mvc 4,Asp.net Ajax,我想在MVC中使用IpagedList在使用ajax的每个页面上调用HttpGet方法 控制器[HttpGet] public ActionResult TestStarted(int TestId,DateTime End_Time,int page=1) { ViewBag.ct = 0; ViewBag.TestId = TestId; var Questions = GetNoOfQuestions(

我想在MVC中使用IpagedList在使用ajax的每个页面上调用HttpGet方法

控制器[HttpGet]

public ActionResult TestStarted(int TestId,DateTime End_Time,int page=1)
        {
            ViewBag.ct = 0;
            ViewBag.TestId = TestId;
            var Questions = GetNoOfQuestions().ToList();

            ViewBag.Questions = Questions;
            EAssessmentNew.BAL.StudentBal studBal = new EAssessmentNew.BAL.StudentBal();
            EAssessmentNew.Dal.Student_Answer_Master _studAnsdal = new EAssessmentNew.Dal.Student_Answer_Master();
            String TestName = studBal.FetchTestName(TestId);
            ViewBag.TestName = TestName;


            ViewBag.EndTime = End_Time;

            List<Question> model = new List<Question>();

            model = new Test_Planning().Fetch_Question_By_Test(TestId);

            ViewBag.total = model.Count();


            if (Request.QueryString["cnt"] != null)
            {
                int count = Convert.ToInt16(Request.QueryString["cnt"].ToString());


                List<int> ChkOptions = studBal.GetCheckedAnswers((int)TestId, model[count].QuestionId, (int)(studBal.getStudentId(Session["sname"].ToString())));
                ViewBag.ChkOptions = ChkOptions;
                int cnt = 0;



                    if (ChkOptions.Count() != 0)
                    {
                        for (int i = 0; i < model[count].Options.Count(); i++)
                        {


                            if (model[count].Options[i].OptionId == ChkOptions.ElementAt(cnt))
                            {
                                model[count].Options[i].IsChecked = true;
                                cnt++;
                            }
                            else
                            {
                                model[count].Options[i].IsChecked = false;
                            }

                            if (cnt >= ChkOptions.Count() - 1)
                            {
                                cnt = ChkOptions.Count() - 1;
                            }

                        }
                    }
                return View(model.OrderByDescending(v => v.Question_Id).ToPagedList(page, 1));
            }
            else
            {
                return View(model.OrderByDescending(v => v.Question_Id).ToPagedList(page, 1));
            }
        }
public ActionResult teststart(int TestId,DateTime End\u Time,int page=1)
{
ViewBag.ct=0;
ViewBag.TestId=TestId;
var Questions=GetNoOfQuestions().ToList();
ViewBag.Questions=问题;
EAssessmentNew.BAL.StudentBal studBal=新EAssessmentNew.BAL.StudentBal();
EAssessmentNew.Dal.Student\u Answer\u Master\u studAnsdal=新EAssessmentNew.Dal.Student\u Answer\u Master();
字符串TestName=studBal.FetchTestName(TestId);
ViewBag.TestName=TestName;
ViewBag.EndTime=结束时间;
列表模型=新列表();
模型=新测试计划()。通过测试获取问题(TestId);
ViewBag.total=model.Count();
if(Request.QueryString[“cnt”!=null)
{
int count=Convert.ToInt16(Request.QueryString[“cnt”].ToString());
列表ChkOptions=studBal.GetCheckedAnswers((int)TestId,model[count].QuestionId,(int)(studBal.getStudentId(Session[“sname”].ToString());
ViewBag.ChkOptions=ChkOptions;
int-cnt=0;
如果(ChkOptions.Count()!=0)
{
对于(int i=0;i=ChkOptions.Count()-1)
{
cnt=ChkOptions.Count()-1;
}
}
}
返回视图(model.OrderByDescending(v=>v.Question_Id).ToPagedList(第1页));
}
其他的
{
返回视图(model.OrderByDescending(v=>v.Question_Id).ToPagedList(第1页));
}
}
我的看法

<script type="text/javascript">

    var TestId ='@ViewBag.TestId'
 function loadQuestions() {
            alert("ok")
            $.ajax({
                url: '@Url.Action("Student","TestStarted")',
                data: { TestId:TestId },
                contentType:"application/json",
                success:function(responce){                   

                }
            });
        }

    </script>


    <div class="pagedList">
            @Html.PagedListPager(Model, page => Url.Action( "",new { onclick="loadQuestions()"}), PagedListRenderOptions.TwitterBootstrapPager)
        </div>

var TestId='@ViewBag.TestId'
函数loadQuestions(){
警报(“正常”)
$.ajax({
url:'@url.Action(“学生”,“测试开始”),
数据:{TestId:TestId},
contentType:“应用程序/json”,
成功:函数(响应){
}
});
}
@PagedListPager(Model,page=>Url.Action(“”,new{onclick=“loadQuestions()”}),pagedListRenderations.TwitterBootstrapPager)

我已经使用IpagedList完成了分页,我想在每个页面上调用控制器的HttpGet方法,但我想在不刷新页面的情况下执行此操作我已经为其编写了ajax,现在我只想知道如何使用@Html.PagedListPager和onClick事件调用该ajax方法,只需更正ajax请求中的url,如下所示:

而不是这个

url: '@Url.Action("Student","TestStarted")'
应该是这样

url: '@Url.Action("TestStarted","Student")'
@Html.PagedListPager
以Html格式生成
'anchor'
标记,以便您可以在文档上放置单击事件,如图所示:-

 $(document).on('click', 'a', function() {
    $.ajax({
        url: this.href,
        type: 'GET',
        datatype: "html",
        data :{  TestId : $("#TestId").val(), End_Time :$("#End_Time").val(), page :$("#page").val() }
        cache: false,
        success: function(result) {
            $('#results').html('');
            $('#results').html(result);
        }
    });
    return false;
});

在上面的代码中,click事件与每个
'anchor'
标记绑定,因此如果需要特定的
'anchor'
标记,则可以将类指定为
$(.pager)。on('click','a',function(){})
这里的
“#results”
是目标div id,其html来自控制器操作。在您的情况下,该id可能不同。

您可以使用
PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(新的AjaxOptions(){HttpMethod=“GET”UpdateTargetId=…)
评论不适用于扩展讨论;此对话已被删除。