如何在MVC 4中的视图和控制器之间临时保存数据

时间:2014-01-30 07:25:01

标签: javascript jquery asp.net-mvc

我在局部视图中遇到了webgrid情况,我希望在最终提交之前保持相同的模型数据。

请找到包含代码的方案

查看:Index.cshtml

function GetFilter() {

    var url = "/Student/SelectStudentByRollNO/?rollno=1";
     var value = $('#rollno').val();



     $.ajax({
         url: url,
         type: 'GET',
         cache: false,
         data: { rollno : value },
         success: function (FeeRemaining) {
             $('#gridContent').html(FeeRemaining);
         }
     });



}



@Html.TextBox("rollno")
<input type="button" id="btn" value="Get filter" onclick="javascript: GetFilter();" />

这里我绑定了gridcontent div id

中的局部视图

部分视图:_studentGrid.cshtml

<div>

@{

var gd = new WebGrid(Model, canPage: true, rowsPerPage: 5, selectionFieldName:    "selectedRow",ajaxUpdateContainerId: "gridContent");
    gd.Pager(WebGridPagerModes.NextPrevious);}




        @gd.GetHtml(tableStyle: "table",
                headerStyle: "head",
                alternatingRowStyle: "altRow",
                selectedRowStyle: "selectRow",
                columns: gd.Columns(
                gd.Column("RollNo", "RollNo"),
                gd.Column("Name", " Name"),
                gd.Column("Branch", "Branch", style: "description"),
                gd.Column("FeeRemaining", "FeeRemaining"),
                gd.Column(header: "Delete",format: @<text><a href="@Url.Action("DeleteStudent",    "Student", new { rollno = item.RollNo })" onclick="javascript:return confirm('Are you sure would you like to delete this Student?');">Delete</a></text>)
          ))


</div>

模型

public class StudentClass {
    public string RollNo { get; set; }
    public string Name { get; set; }
    public string Branch { get; set; }
    public long FeeRemaining { get; set; }
}

控制器&amp;行动结果

public class StudentController : Controller {
    //
    // GET: /Student/

    public ActionResult Index() {

        return View();
    }

    public ActionResult SelectStudentByRollNO(int rollno)
    {
        List<StudentClass> lststudent = new List<StudentClass>();
        lststudent.Add(new StudentClass { RollNo = "08330001", Name = "Surbhi", Branch = "C.S", FeeRemaining = 18000 });
        lststudent.Add(new StudentClass { RollNo = "08330004", Name = "Arun", Branch = "C.S", FeeRemaining = 2500 });
        lststudent.Add(new StudentClass { RollNo = "08329006", Name = "Ankita", Branch = "I.T", FeeRemaining = 31000 });

        ViewBag.lst_student = lststudent;

        return PartialView("_studentGrid", lststudent);

    }

    public ActionResult DeleteStudent(string rollno)
    {

        List<StudentClass> FeeRemaining = new List<StudentClass>();


        return PartialView("_studentGrid", FeeRemaining);
    }
}

在上面的Delete DeleteStudent Action结果中我想要所有的原始数据......

提前致谢 市政

1 个答案:

答案 0 :(得分:0)

如果您在客户端浏览器上呈现原始数据然后将其发送回服务器,则必须对其进行全部验证,因为用户在将其发回给您之前可能已经更改了任何内容。永远不要相信已经访问过浏览器的数据。看起来您希望在“SelectStudentByRollNO”调用中访问数据存储(例如数据库)。删除数据后,您可以调用:

return RedirectToAction("SelectStudentByRollNO", new { rollno = 99 });

这将返回SelectStudentByRollNO的结果。

另外,您可能希望将“DeleteStudent”上的“rollno”更改为与“SelectStudentByRollNO”匹配的int。