我是ASP.Net MVC的新手。我有一个名为Index.cshtml
的视图。我在homeController
,'Index'
和'saveAttendance'
中有两个操作。首先,进行索引操作,并从'索引'返回的视图中返回数据。行动被发送到' saveAttendance'行动。完成' saveAttendance'中的所有功能之后动作完成我需要返回查看' Index.cshtml'在viewbag中成功的消息。我没有将视图分配给' saveAttendance'行动。我只需要返回查看索引'动作。
我的家庭控制器代码:
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";
}
}
答案 0 :(得分:0)
将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到正确的操作。
答案 1 :(得分:0)
问题是函数的返回类型“saveAttendance”这是无效的,并且在“ saveAttendance ”的末尾你正在做RedirectToAction("Index")
,最终调用索引ActionResult但由于“saveAttendance”无效,因此您不会被重定向到索引视图。
只做三个小调整
将public void saveAttendance(attendance_entry entryObj)
更改为public ActionResult Index(attendance_entry entryObj)
在 saveAttendance 函数结束时才写
Return RedirectToAction("Index");
代替
RedirectToAction("Index");
使用[ChildActionOnly]
以便网址无法访问 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";
}
}`