2016-05-18 8 views
1

Я пытаюсь сделать так, чтобы после каждого входа пользователя перейти на главную страницу. Но мне нужно включить View/Car/Index. Я пытаюсь поставить только путь, но он не работает. Есть идеи? Вот мой AccountController:После перехода на главную страницу C# MVC

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using Automarket.Models; 
using System.Data.Entity.Spatial; 

namespace Automarket.Controllers 
{ 

    public class AccountController : Controller 
    { 
     // GET: Account 
     public ActionResult Index() 
     { 
      using (OurDBContext db = new OurDBContext()) 
      { 
       return View(db.userAccount.ToList()); 
      } 
     } 

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

     [HttpPost] 
     public ActionResult Register(UserAccount account) 
     { 
      if (ModelState.IsValid) 
      { 
       using (OurDBContext db = new OurDBContext()) 
       { 
        db.userAccount.Add(account); 
        db.SaveChanges(); 
       } 
       ModelState.Clear(); 
       ViewBag.Message = account.Firstname + " " + account.Lastname + "Succesfully Registered: "; 
      } 
      return View(); 
     } 
     // Login method 
     public ActionResult Login() 
     { 
      return View(); 
     } 

     [HttpPost] 

     public ActionResult Login(UserAccount user) 
     { 
      using (OurDBContext db = new OurDBContext()) 
      { 
       var usr = db.userAccount.Single(u => u.Username == user.Username && u.Password == user.Password); 
       if (usr != null) 
       { 
        Session["UserID"] = usr.Id.ToString(); 
        Session["Username"] = usr.Username.ToString(); 
        return RedirectToAction("Logged In "); 

       } 
       else 
       { 
        ModelState.AddModelError(" ", "Username or Password are incoorect"); 
       } 
      } 
      return View(); 
     } 


     public ActionResult LoggedIn() 
     { 
      if (Session["UserID"] != null) 
      { 
       return View(); 
      } 
      else 
      { 
       return RedirectToAction("Login"); 
      } 

     } 
     [HttpGet] 
     public ActionResult Create() 
     { 
      return View(); 
     } 

     [HttpPost] 
     public ActionResult Create(UserAccount account) 
     { 
      using (OurDBContext db = new OurDBContext()) 
       if (ModelState.IsValid) 
       { 
        db.userAccount.Add(account); 
        return RedirectToAction("Index"); 
       } 
      return View(account); 
     } 

     // List the details of user 
     public ActionResult Details(int id=0) 
     { 
      using (OurDBContext db = new OurDBContext()) 
      { 
       UserAccount user = db.userAccount.Find(id); 
       if (user==null) 
       { 
        return HttpNotFound(); 
       } 
       return View(user); 
      } 
     } 

     // post method for Delete User 
     public ActionResult Delete(int id =0) 
     { 
      using (OurDBContext db = new OurDBContext()) 
      { 
       UserAccount user = db.userAccount.Find(id); 
       if (user == null) 
       { 
        return HttpNotFound(); 
       } 
       return View(user); 
      } 
     } 

     [HttpPost,ActionName("Delete")] 
     public ActionResult DeleteConfirmed(int id=0) 
     { 
      using (OurDBContext db = new OurDBContext()) 
      { 
       UserAccount user = db.userAccount.Find(id); 
       if (user==null) 
       { 
        return HttpNotFound(); 
       } 
       db.userAccount.Remove(user); 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 
     } 

     public ActionResult Edit(int? id) 
     { 
      using (OurDBContext db = new OurDBContext()) 
      { 
       if (id == null) 
       { 
        return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest); 
       } 
       UserAccount user = db.userAccount.Find(id); 
       if (user == null) 
       { 
        return HttpNotFound(); 
       } 
       return View(user); 
      } 
     } 


     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Edit([Bind(Include = "Id,Firstname,Lastname,Email,Username,Password,ConfirmaPassword")] UserAccount user) 
     { 
      using (OurDBContext db = new OurDBContext()) 
      if (ModelState.IsValid) 
      { 
       db.Entry(user).State = System.Data.Entity.EntityState.Modified; 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 
      return View(user); 
     } 

    } 
} 

Индекс Вид:

@model IEnumerable<Automarket.Models.UserAccount> 

@{ 
    ViewBag.Title = "Index"; 
} 



<h2>Index</h2> 

<p> 
    @Html.ActionLink("Create New", "Register") 

</p> 
<table class="table"> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.Firstname) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Lastname) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Email) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Username) 
     </th> 

     <th></th> 
    </tr> 

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.Firstname) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Lastname) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Email) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Username) 
     </td> 

     <td> 
      @Html.ActionLink("Edit", "Edit", new { id=item.Id }) | 
      @Html.ActionLink("Details", "Details", new { id=item.Id }) | 
      @Html.ActionLink("Delete", "Delete", new { id=item.Id }) 
     </td> 
    </tr> 
} 

</table> 

ответ

1
public ActionResult LoggedIn() 
    { 
     if (Session["UserID"] != null) 
     { 
      return View(); 
     } 
     else 
     { 
      return RedirectToAction("Login"); 
     } 

    } 

Я думаю, вы должны поставить имя точки зрения, что вы хотите показать в "вернуться View (" nameOfViewHere ")".

0

return RedirectToAction("Index", "Car");

Вы должны передать имя контроллеры тоже, как вы звоните с другого контроллера

Смежные вопросы