【问题标题】:Passing message in ViewBag from controller to view in ASP.Net MVC将 ViewBag 中的消息从控制器传递到 ASP.Net MVC 中的视图
【发布时间】: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


【解决方案1】:

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 到正确的操作。

【讨论】:

  • 返回视图();最后一行显示错误:无法将方法组转换为非委托类型'ActionResult'
  • 对不起,先生,我没听懂您的意思。我将动作名称更改为“索引”动作。
  • 这个方法还是无效的吗?
  • 我把它改成了 ActionResult : public ActionResult Index(attendance_entry entryObj)
  • 我改变了 return View();返回视图(“索引”);这对我有用。谢谢大佬帮忙
【解决方案2】:

问题是函数 "saveAttendance" 的返回类型是无效的,并且在 "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";
            }

        }`

【讨论】:

    猜你喜欢
    • 2018-11-23
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多