2017-04-20 87 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"; 
      } 

     }` 
相關問題