![](/img/trans.png)
[英]MVC 5 asp.net, viewbag from controller to view is not working
[英]Passing message in ViewBag from controller to view in ASP.Net MVC
我是ASP.Net MVC的新手。 我有一個名為Index.cshtml
的視圖。我在homeController
有兩個動作, 'Index'
和'saveAttendance'
。 首先,進行索引操作,並將“索引”操作返回的視圖中的數據發送到“saveAttendance”操作。 完成'saveAttendance'操作中的所有功能后,我需要返回查看“Index.cshtml”並在viewbag中顯示成功消息。 我沒有將視圖分配給“saveAttendance”操作。 我只需要返回查看“索引”動作。
我的homeController的代碼:
public ActionResult Index()
{
try
{
ViewBag.nepali_date = dc.ToBS(DateTime.Now);
}
catch (Exception ex)
{
throw ex;
}
return View();
}
public void saveAttendance(attendance_entry entryObj)
{
try
{
DateConverter dc = new DateConverter();
DateTime current_date = entryObj.current_date;
string nep_date = entryObj.nep_date;
DateTime current_time = entryObj.current_time;
string current_day = entryObj.current_day;
int staff_id = Convert.ToInt32(Session["staff_id"]);
string in_time = entryObj.in_time;
string out_time = entryObj.out_time;
if( DAL.Attendance.Model.exists(staff_id.ToString())!=0)
{
ViewBag.message = "Attendance for today is already made.";
return;
}
DAL.Attendance.Model.insert(nep_date, staff_id,current_date, current_time, current_day,in_time,out_time);
ViewBag.message = "Record saved successfully";
RedirectToAction("Index");
}
catch (Exception)
{
ViewBag.message = "Failed to save attendance record";
}
}
將saveAttenance
重命名為Index
以處理POST。
public ActionResult Index() {
ViewBag.nepali_date = dc.ToBS(DateTime.Now);
return View();
}
[HttpPost]
public ActionResult Index(attendance_entry entryObj) {
try {
var dc = new DateConverter();
var current_date = entryObj.current_date;
var nep_date = entryObj.nep_date;
var current_time = entryObj.current_time;
var current_day = entryObj.current_day;
var staff_id = Convert.ToInt32(Session["staff_id"]);
var in_time = entryObj.in_time;
var out_time = entryObj.out_time;
if( DAL.Attendance.Model.exists(staff_id.ToString())!=0) {
ViewBag.message = "Attendance for today is already made.";
} else {
DAL.Attendance.Model.insert(nep_date, staff_id,current_date, current_time, current_day,in_time,out_time);
ViewBag.message = "Record saved successfully";
}
} catch (Exception) {
ViewBag.message = "Failed to save attendance record";
}
ViewBag.nepali_date = dc.ToBS(DateTime.Now);
return View();
}
並在視圖中更新表單以POST到正確的操作。
問題是函數“saveAttendance”的返回類型是無效的,在“ saveAttendance ”結束時你正在做RedirectToAction("Index")
,它最終調用索引ActionResult,但是當“saveAttendance”為void時你不會被重定向到Index視圖。
只需做三個小調整
將public void saveAttendance(attendance_entry entryObj)
更改為public ActionResult Index(attendance_entry entryObj)
在saveAttendance函數結束時只需寫入Return RedirectToAction("Index");
而不是RedirectToAction("Index");
使用[ChildActionOnly]
以便url不訪問saveAttendance
這是代碼
[ChildActionOnly]
public ActionResult saveAttendance(attendance_entry entryObj)
{
try
{
DateConverter dc = new DateConverter();
DateTime current_date = entryObj.current_date;
string nep_date = entryObj.nep_date;
DateTime current_time = entryObj.current_time;
string current_day = entryObj.current_day;
int staff_id = Convert.ToInt32(Session["staff_id"]);
string in_time = entryObj.in_time;
string out_time = entryObj.out_time;
if (DAL.Attendance.Model.exists(staff_id.ToString()) != 0)
{
ViewBag.message = "Attendance for today is already made.";
return;
}
DAL.Attendance.Model.insert(nep_date, staff_id, current_date, current_time, current_day, in_time, out_time);
ViewBag.message = "Record saved successfully";
return RedirectToAction("Index");
}
catch (Exception)
{
ViewBag.message = "Failed to save attendance record";
}
}`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.