Javascript 如何在MVC4中在视图和控制器之间临时保留数据
我在局部视图中遇到了一个webgrid情况,我希望在最终提交之前维护相同的模型数据 请用代码查找场景 查看:Index.cshtmlJavascript 如何在MVC4中在视图和控制器之间临时保留数据,javascript,jquery,asp.net-mvc,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',
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();" />
<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; }
}
控制器和动作结果
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);
}
}
公共类学生控制器:控制器{
//
//获得:/Student/
公共行动结果索引(){
返回视图();
}
公共行动结果SelectStudentByRollNO(int rollno)
{
List lststudion=新列表();
添加(新的StudentClass{RollNo=“08330001”,Name=“Surbhi”,Branch=“C.S”,feelaining=18000});
添加(新的StudentClass{RollNo=“08330004”,Name=“Arun”,Branch=“C.S”,feelaining=2500});
添加(新的StudentClass{RollNo=“08329006”,Name=“Ankita”,Branch=“I.T”,feelaining=31000});
ViewBag.lst_student=lstsudent;
返回PartialView(“_studentGrid”,lststuent);
}
public ActionResult DeleteStudent(字符串rollno)
{
List feelaining=新列表();
返回部分视图(“学生网格”,剩余);
}
}
在上述删除删除学生行动结果中,我需要所有原始数据
提前谢谢
muni如果在客户端浏览器上呈现原始数据,然后将其发送回服务器,则必须对其进行验证,因为用户可能会在将其发回给您之前更改任何内容。永远不要信任已发送到浏览器并返回的数据。看起来您希望在“SelectStudentByRollNO”调用中访问数据存储(如数据库)。删除数据后,您可以调用:
return RedirectToAction("SelectStudentByRollNO", new { rollno = 99 });
这将返回SelectStudentByRollNO的结果
另一方面,您可能希望将“DeleteStudent”上的“rollno”更改为int,以匹配“SelectStudentByRollNO”。亲爱的@user3251962,您为什么要将此问题标记为“java”?Java和JavaScript有很多共同点,就像car和地毯一样。。最后,我使用session对象临时存储值