如何在登录mvc 4后从db生成用户配置文件

时间:2016-03-10 12:56:34

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我想在登录成功后创建登录页面,它应该转到从数据库生成的用户配置文件。这是我的家庭控制器。

namespace webSite.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Login(User u)
        {
            // this action is for handle post (login)
            if (ModelState.IsValid) // this is check validity
            {
                using (projectEntities dc = new projectEntities())
                {
                    var v = dc.service_provider.Where(a => a.Sp_email.Equals(u.Email) && a.Sp_password.Equals(u.Password)).FirstOrDefault();
                    if (v != null)
                    {
                        return RedirectToAction("AfterLogin");
                    }
                }
            }
            return View(u);
        }


        public ActionResult AfterLogin()
        {
            return View();
        }

        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Register(service_provider U)
        {
            if (ModelState.IsValid)
            {
                using (projectEntities dc = new projectEntities())
                {
                    //you should check duplicate registration here 
                    if (U != null)
                    {
                        dc.service_provider.Add(U);
                        dc.SaveChanges();
                        ModelState.Clear();
                        U = null;
                        ViewBag.Message = "Successfully Registration Done";
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
            }
            return View(U);
        }
    }
}

我想在登录后生成用户个人资料。

1 个答案:

答案 0 :(得分:0)

        public ActionResult Login(User u)
        {
            // this action is for handle post (login)
            if (ModelState.IsValid) // this is check validity
            {
                using (projectEntities dc = new projectEntities())
                {
                    var v = dc.service_provider.Where(a => a.Sp_email.Equals(u.Email) && a.Sp_password.Equals(u.Password)).FirstOrDefault();
                    if (v != null)
                    {
                        //return RedirectToAction("AfterLogin");

                        int id = u.ID; // assign your valid user's id and pass it to AfterLogin Action
                        return RedirectToAction("AfterLogin", new {@id=id});
                    }
                }
            }
            return View(u);
        }


    public ActionResult AfterLogin(int id)
    {
        User u = // select your user's data using id and assign to a user object 
        return View(u); // and pass the user object to view
    }

在视图中 -

@model Project.DataObject.User // path to User
@{
    ViewBag.Title = "User Profile";        
}

<input type="text" id="txtName" value="@Model.Name"> //Get user's name