2017-04-20 86 views
1

我是ASP.Net MVC的新手。我有一个名为Index.cshtml的视图。我在homeController'Index''saveAttendance'中有两个操作。首先,索引操作发生,'索引'操作返回的视图中的数据被发送到'saveAttendance'操作。在'saveAttendance'动作的所有功能完成后,我需要返回查看'Index.cshtml'在viewbag中的成功消息。我没有分配给“saveAttendance”操作的视图。我只需要返回查看'索引'行动。在控制器的ViewBag中传递消息以在ASP.Net中查看MVC

我的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";  
    } 

} 
+0

然后让saveAttenance返回索引视图'返回视图( “指数”)'或重命名'saveAttenance'来'Index'来处理POST – Nkosi

回答

0

重命名saveAttenanceIndex处理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到正确的操作。

+0

return View();在最后一行显示我的错误:无法将方法组转换为非委托类型'ActionResult' –

+0

您更新了方法签名吗? – Nkosi

+0

对不起,先生,但我没有得到你。我将操作名称改为“索引”操作。 –

0

的问题是功能“saveAttendance”返回类型是void,并且在“saveAttendance”你到底在做RedirectToAction("Index")最终调用索引的ActionResult但“saveAttendance”是无效的你将不会被重定向到索引视图。

只是让三个小的调整

  1. 变化public void saveAttendance(attendance_entry entryObj)public ActionResult Index(attendance_entry entryObj)

  2. saveAttendance结束功能只写 Return RedirectToAction("Index"); 而不是 RedirectToAction("Index");

  3. 使用[ChildActionOnly]使saveAttendance不是由URL

访问这里是代码

[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"; 
      } 

     }` 
相关问题