简体   繁体   English

将ViewBag中的消息从控制器传递到ASP.Net MVC中的视图

[英]Passing message in ViewBag from controller to view in ASP.Net MVC

I am new to ASP.Net MVC . 我是ASP.Net MVC的新手。 I have a view named Index.cshtml .I have two actions in homeController , 'Index' and 'saveAttendance' . 我有一个名为Index.cshtml的视图。我在homeController有两个动作, 'Index''saveAttendance' First, Index action takes place and the datas from view returned by 'Index' action is sent to 'saveAttendance' action. 首先,进行索引操作,并将“索引”操作返回的视图中的数据发送到“saveAttendance”操作。 After all the functions in 'saveAttendance' action is completed I need to return to view to 'Index.cshtml' with a success message in viewbag. 完成'saveAttendance'操作中的所有功能后,我需要返回查看“Index.cshtml”并在viewbag中显示成功消息。 I don't have a view assigned to 'saveAttendance' action. 我没有将视图分配给“saveAttendance”操作。 I just need to return to view in 'Index' action. 我只需要返回查看“索引”动作。

My homeController's code: 我的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";       
    }

}

Rename saveAttenance to Index to handle POST. 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();
}

And update the form in the view to POST to the the correct action. 并在视图中更新表单以POST到正确的操作。

The problem is the return type of function "saveAttendance" which is void and in the end of " saveAttendance " you are doing RedirectToAction("Index") which eventually calls the index ActionResult but as "saveAttendance" is void you wont be redirected to Index View. 问题是函数“saveAttendance”的返回类型是无效的,在“ saveAttendance ”结束时你正在做RedirectToAction("Index") ,它最终调用索引ActionResult,但是当“saveAttendance”为void时你不会被重定向到Index视图。

Just make three small tweaks 只需做三个小调整

  1. Change public void saveAttendance(attendance_entry entryObj) to public ActionResult Index(attendance_entry entryObj) public void saveAttendance(attendance_entry entryObj)更改为public ActionResult Index(attendance_entry entryObj)

  2. In the end of saveAttendance function just write Return RedirectToAction("Index"); saveAttendance函数结束时只需写入Return RedirectToAction("Index"); instead of RedirectToAction("Index"); 而不是RedirectToAction("Index");

  3. Use [ChildActionOnly] so that saveAttendance is not accessed by url 使用[ChildActionOnly]以便url不访问saveAttendance

Here is the code 这是代码

[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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM