其他ajax请求后,Ajax请求不会运行

时间:2014-11-28 18:15:44

标签: jquery .net ajax asp.net-mvc

当我在我的页面上转到 localhost:52007 / Home / Exercise / 5 时,然后显示表单和分页。当我单击提交按钮时,脚本正在运行并且所有工作。但是当我想转到其他页面而不是单击“提交”按钮时,则显示其他表单。并且有问题,因为在那里我点击提交按钮(在第二页)然后脚本没有运行。只有页面刷新和我的网址看起来像

  

本地主机:52007 /家庭/运动/ 5项%5B%5D =安培;项%5B%5D =安培;项%5B%5D =安培;项%5B%5D =安培;项%5B%5D =&安培;项%5B%5D =安培;项%5B%5D =安培;项%5B%5D =安培;项%5B%5D =安培;项%5B%5D = *

简而言之,当动作练习返回PartialView时,第一个脚本不起作用。

public ActionResult Exercise(int? id, int page = 1)
{        
     var model = _db.Tasks.Where(o => o.Tense.Id == id).OrderBy(o => o.Id).Skip(page-       1).Take(1).Single();
     TaskModel tasklistviewmodel = new TaskModel();
     tasklistviewmodel.Task = model;
     tasklistviewmodel.Questions = model.Questions.Split(';').ToList();
     tasklistviewmodel.TotalTasks = _db.Tasks.Where(o => o.Tense.Id == id).Count();

     if(Request.IsAjaxRequest())
     {
         return PartialView("_exercises", tasklistviewmodel);
     }
     return View(tasklistviewmodel);
}

public ActionResult Check(string[] answers, int id)
{
     var model = _db.Tasks.Where(o => o.Id == id).SingleOrDefault();
     TaskModel tasklistviewmodel = new TaskModel();
     tasklistviewmodel.Task = model;
     tasklistviewmodel.AnswersUsers = answers.ToList();
     tasklistviewmodel.Questions = model.Questions.Split(';').ToList();
     tasklistviewmodel.Answers = model.Answers.Split(';').ToList();
     return PartialView("_exercises", tasklistviewmodel);
}

jquery的:

$('form').submit(function () {
   var values = [];
   $("input[name='items[]']").each(function () {
       values.push($(this).val());
   });
   var $id = $("#hiddenId").val();
   $.ajax({
       url: '/Home/Check',
       traditional: true,
       data: { answers: values, id: $id },
       type: 'POST'
   })
   .done(function (data) {
       $("#exercises").replaceWith(data);
   });
   return false;
});

$(document).on("click", ".pagination a", function () {
   var $a = $(this);
   $.ajax({
       url: $a.attr('href'),
       type: 'get'
   })
   .done(function (data) {
        $("#exercises").replaceWith(data);
   });
   return false;
});

html Exercise.cshtml:

<div class="col-md-12">
<div class="text-center">
    <ul class="pagination">
        @for (int i = 1; i <= Model.TotalTasks; i++)
        {
            <li><a href="@Url.Action("Exercise","Home", new{page = i})">@i</a></li>
        }
    </ul>
</div>
@Html.Partial("_exercises", Model)

html _exercise.cshtml

<div id="exercises">
if (Model.Answers.Count > 0)
{
    <div class="alert alert-info">Twój wynik to: @Model.CountPoints()</div>
    <form>
        @for (int i = 0, j = 0; i < Model.Questions.Count; i = i + 2, j++)
        {
            <p>@Model.Questions[i] <input type="text" name="items[]" value="@Model.AnswersUsers[j]" class="inputForm inputForm-type1" /> @Html.Raw(Model.Questions[i + 1])</p>
            if (Model.Answers[j] != Model.AnswersUsers[j]) { <div class="correctAnswer">Poprawna odpowiedź: @Model.Answers[j]</div> }
        }
        <button type="submit" class="btn btn-info">Sprawdź</button>
    </form>
}
else
{
    <form>
        <input type="hidden" value="@Model.Task.Id" id="hiddenId" />
        @for (int i = 0, j = 1; i < Model.Questions.Count; i = i + 2, j++)
        {
            <p>@j. @Model.Questions[i] <input type="text" name="items[]" class="inputForm inputForm-type1"/> @Html.Raw(Model.Questions[i + 1])</p>
        }
        <button type="submit" class="btn btn-info" >Sprawdź</button>
    </form>
}
</div>

0 个答案:

没有答案