【发布时间】:2017-04-20 19:46:37
【问题描述】:
我是 ASP.Net MVC 的新手。我有一个名为Index.cshtml 的视图。我在homeController、'Index' 和'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 返回索引视图
return View("Index")或将saveAttenance重命名为Index以处理POST
标签: c# asp.net-mvc-4