如何防止部分视图在提交后消失?

时间:2014-06-06 00:17:14

标签: ajax asp.net-mvc-3

我在子视图中使用BeginForm。当我提交表单时,我仍然在父视图上,但子视图消失了。我希望提交信息,但表单将保留,直到用户想要加载另一个部分视图或完全离开页面。有没有办法阻止页面在提交时消失?这是我的BeginForm的样子:

 using (Html.BeginForm("Action", "Controller", new { fromTeacherPage = true, searchTeacher =     instructorName, selectedDepartment = Model.Assignments.FirstOrDefault().departmentNumber, id = Model.Assignments.FirstOrDefault().InstructorId, strCategoryName = @ViewBag.categoryname }, FormMethod.Post, new { @name = "formName", @class = "nameOfClass" }))
{

 //code for form here

 <button id="submitButton" class="submitButton">Submit</button><br />
  }

更新:

   <script type="text/javascript">
     $(document).ready(function () {
    $("#datep").datepicker({ showOn: "both", buttonText: "Select Date", changeMonth: true,      changeYear: true, yearRange: "-2:+2", showOtherMonths: true, onSelect: function (date, datepickder) {
        var sltdDate = { selectedDate: date };

        $.ajax({
            type: "GET",
            url: "/Schedule/GetSchedule",
            data: sltdDate,

            datatype: "html",
            success: function (data) {
                $("#returnedData").html(data);
                $("#returnedData #dateContainer").remove();
                $("<button>Hide</button>").appendTo("#homeworkUpdateId-0")


            }
        });

    }




       });

   });
 </script>
 @{

     int? intTeacherID = Convert.ToInt32(HttpContext.Current.Session["intTeacherId"]);
     string instructorName = (from x in Model.Enrollments where x.InstructorId == intTeacherID select x.InstructorFullName).FirstOrDefault();



   }




 <div id="dateContainer">
   <label for ="datep">Date: </label><input id="datep" />

   </div>

    <div id="returnedData">


   @if (Model.Assignments != null)
   {



      using (Html.BeginForm("Action", "Controller", new { fromTeacherPage = true, searchTeacher = instructorName, selectedDepartment = Model.Assignments.FirstOrDefault().departmentNumber, id = Model.Assignments.FirstOrDefault().teacherId, strCategoryName = @ViewBag.categoryname }, FormMethod.Post, new { @name = "formName", @class = "submitAttendance" }))
    {


 <table>
     <tr>
        <th>
          Grade
     </th>



    <th>
      Attendance
    </th>


    <th>
    Clas Day
    </th>
    <th>
   Assignment Type
    </th>
    <th>
   Overall Grade
    </th>
    </tr>

     @foreach (var assignment in Model.Assignments.Select((x, i) => new { Data = x, Index = i }))
     {









    int asgnIndex = assignment.Index;

    <tr id="rowId+@asgnIndex">

    <td>


               <div id="homeworkUpdateId-@asgnIndex">
                  @Html.TextBox("HomeworkGrade", assignment.Data.HomeworkGrade.ToString(), new { style = "width:55px; text-align: center" })
               </div>
    </td>

     </table>
    <button id="submitButton" class="submitButton">Submit </button><br />
   }
  }

  </div>

1 个答案:

答案 0 :(得分:1)

您正在使用

using(Html.BeginForm()){} 

如果您只想重新加载视图中必须使用的部分

,这将刷新整个页面
using (Ajax.BeginForm("Action", "Controller", null, new AjaxOptions {UpdateTargetId = "divToUpdate", InsertionMode = InsertionMode.Replace, HttpMethod = "GET"}, new {id = "someIdFOrm"}))