如何在插入学生后在同一浏览器上显示消息?目前我正在使用返回内容导航我到一个新页面,但我想在同一页面上显示该消息并保持在同一页面上:
索引控制器:
public ActionResult Index()
{
return View(_repository.ListAll().OrderByDescending(s => s.StudentID));
}
控制器操作:
public ActionResult RemoveStudent(int id)
{
StudentDataContext student= new StudentDataContext();
var std = student.Students.Single(s => s.StudentID == id);
student.Students.DeleteOnSubmit(std);
student.SubmitChanges();
return Content("Student " + std.StudentId.ToString() + " Removed");
}
提前致谢
答案 0 :(得分:4)
返回您的视图,您可以将消息存储在模型或TempData中。
public ActionResult RemoveStudent(int id)
{
StudentDataContext student= new StudentDataContext();
var std = student.Students.Single(s => s.StudentID == id);
student.Students.DeleteOnSubmit(std);
student.SubmitChanges();
TempData["Message"] = "Student " + std.StudentId.ToString() + " Removed";
return RedirectToAction("Index");
}
在您的视图中,您可以检查TempData["Message"]
是否为空并显示它。
答案 1 :(得分:1)
您可以返回一些特定的视图:
public ActionResult RemoveStudent(int id)
{
StudentDataContext student= new StudentDataContext();
var std = student.Students.Single(s => s.StudentID == id);
student.Students.DeleteOnSubmit(std);
student.SubmitChanges();
ViewBag.Message = "Student " + std.StudentId.ToString() + " Removed";
return View();
// or if you want to specify a view name:
// return View("MyView");
// and if you need to pass a model that the view expects
// return View("MyView", someModel);
}
并在视图中:
<div>@ViewBag.Message</div>
答案 2 :(得分:1)
您可以使用AJAX异步执行此操作。你可以使用JQuery做到这一点:
http://blog.bobcravens.com/2009/11/ajax-calls-to-asp-net-mvc-action-methods-using-jquery/
或ajax形式:
http://www.hanselman.com/blog/ASPNETMVCPreview4UsingAjaxAndAjaxForm.aspx