将模型从一个动作传递到同一控制器中的另一个动作 [英] passing model from one action to another action in same controller

查看:77
本文介绍了将模型从一个动作传递到同一控制器中的另一个动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.......

我有一个这样的viewmodel ...........

Hi everyone .......

i have a viewmodel like this...........

public class ResultViewModel
    {
        public IEnumerable<Users> users { get; set; }
    }

    public class Users
    {
        public int UserID { get; set; }
        public string  UserName { get; set; }
        public string Email { get; set; }
        public DateTime Date    { get; set; }
        public int DeptID { get; set; }
    }



控制器是



and Controller is

[HttpPost]
        public ActionResult ServiceOrderManager(SOMViewModel model)
        {
            DateTime date = Convert.ToDateTime(model.Date);
            List<int> items = new List<int>();
            foreach (var objItem in model.Departments)
            {
                if (objItem.IsSelected)
                {
                    //you can get the selected item id here
                    items.Add(objItem.DeptID);
                }
            }
            List<Users> us = new List<Users>();
            foreach (var item in items)
            {
                var list = from u in db.tbl_User
                           join ul in db.tbl_User_Login on u.UserID equals ul.User_ID
                           join d in db.tbl_Departments on ul.User_ID equals d.User_id
                           join r in db.tbl_Roles on u.Role_ID equals r.Role_ID
                           join p in db.tbl_PreCodes on u.Pre_Code_ID equals p.Pre_Code_ID
                           join w in db.tbl_WMPages on u.WM_Page_ID equals w.WM_Page_ID
                           where u.CreatedDate == date || d.Department_ID == item
                           select new { u.UserID, u.Name, u.EmailID, u.CreatedDate, d.Department_ID };
                var lst = list.ToList();
                if (lst.Count > 0)
                {
                    foreach (var i in lst)
                    {
                        Users user = new Users
                   {
                       UserID = Convert.ToInt32(lst.ElementAt(0).UserID),
                       UserName = lst.ElementAt(0).Name,
                       Email = lst.ElementAt(0).EmailID,
                       Date = Convert.ToDateTime(lst.ElementAt(0).CreatedDate),
                       DeptID = Convert.ToInt32(lst.ElementAt(0).Department_ID)
                   };
                        us.Add(user);
                    }
                }
            }
            var resultmodel = new ResultViewModel
            {
                users = us.ToList()
            };

            return RedirectToAction("ServiceOrderManagerResult", "Login",resultmodel);
        }

        public virtual ActionResult ServiceOrderManagerResult(ResultViewModel model)
        {
            List<ResultViewModel> lst = new List<ResultViewModel>();
            lst.Add(model);
            return View(lst);
        }




当我将模型传递给第二个动作时.......模型消失了.....
我如何将模型传递给其他动作.......




when i am passing the model to the second action ....... the model goes to nill.......
how can i pass the model to another action.......

推荐答案


您可以为此目的使用TempData,TempData将仅保留一次重定向,并在随后的请求中自动退出,而Session将在该会话的所有HTTP请求中保持不变.

您还可以通过查询字符串来使用,但这可能会在您遇到问题时出现问题,因为您正在撒尿数据列表并且查询字符串的数据有限


参考:
tempdata
tempdata

http: //stackoverflow.com/questions/6543678/is-it-possible-to-redirect-to-other-action-passing-it-well-our-current-mode [
Hi ,
You can use TempData for this purpose ,TempData will survive only a single redirect and be automatically evicted on the subsequent request whereas Session will be persistent across all HTTP requests for the session.

you can use also pass using query string but it is may create problem in your case as you are pissing list of data and query string has limited data


refer:
tempdata
tempdata

http://stackoverflow.com/questions/6543678/is-it-possible-to-redirect-to-another-action-passing-it-as-well-our-current-mode[^]


这篇关于将模型从一个动作传递到同一控制器中的另一个动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆