2013-10-28 72 views
0

我正在使用實體框架6.0(代碼優先)在ASP.NET MVC4的登錄/註冊系統,我想知道我應該如何正確處理POST-s。ASP.NET MVC4處理POST模型

我的用戶模型:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.ComponentModel.DataAnnotations; 

namespace Autokereskedes.Models 
{ 
    public class User 
    { 
     //Saját Kulcs 
     [Key] 
     public int UserId { set; get; } 

     //Külső kulcsok 

     //Model hivatkozások 
     public List<Reservation> Reservations { set; get; } 

     //Egyedi elemek 
     [Required] 
     [EmailAddress] 
     [StringLength(254)] 
     [Display(Name = "E-mail")] 
     public string Email { set; get; } 

     [Required] 
     [DataType(DataType.Password)] 
     [StringLength(100,MinimumLength=4)] 
     [Display(Name = "Jelszó")] 
     public string Password { set; get; } 

     public string Phone { set; get; } 

     public Boolean Banned { set; get; } 
     public string Country { set; get; } 
     public string City { set; get; } 
     public string Street { set; get; } 
     public int? ZipCode { set; get; } 
     public DateTime RegistrationDate { set; get; } 
     public DateTime? LastLoginDate { set; get; } 
     public DateTime? PasswordChangedDate { set; get; } 


    } 
} 

我的登錄功能:

[HttpPost] 
public ActionResult LogIn(User user) 
{ 
    if (ModelState.IsValid) 
    { 
     if (IsUserDataValid(user.Email, user.Password)) 
     { 
      FormsAuthentication.SetAuthCookie(user.Email, user.???) 
     } 
    } 

    return View(); 
} 

我要設置永久性的Cookie,基於在登錄表單的複選框,但我的用戶模型沒有有一個public Boolean StayLoggedIn;屬性,我不希望這個選項也存儲在我的數據庫中。我該如何處理?

我的登錄表單:

@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary(true, "Sikertelen belépés, ellenőrizze adataid!"); 

    <div>@Html.LabelFor(u => u.Email)</div> 
    <div class="input-control text"> 
     @Html.TextBoxFor(u => u.Email, new { @placeholder = "Írja be az email címét"}) 
     @Html.ValidationMessageFor(u => u.Email) 
     <button class="btn-clear"></button> 
    </div> 
    <div>@Html.LabelFor(u => u.Password)</div> 
    <div class="input-control password"> 
     @Html.TextBoxFor(u => u.Password, new { @placeholder = "Írja be jelszavát" }) 
     @Html.ValidationMessageFor(u => u.Password) 
     <button class="btn-reveal"></button> 
    </div> 
    <label class="input-control checkbox"> 
     <input type="checkbox"> 
     <span class="helper">Bejelentkezve marad</span> 
    </label> 
    <input type="submit" value="Bejelentkezés" /> 
} 

回答

0

我認爲你必須爲你創造另一種模式的默認帳戶模型類登錄頁面:

class LogInModel 
{ 
    public string UserName { get; set; } 
    public string Pass { get; set; } 

    public bool StayLoggedIn { get; set; } 
} 

並基於此模型創建您的視圖,並在控制器中檢索此模式。

在登錄頁面中使用用戶模型並不是一個好主意。

+0

我想我會隨之而去。謝謝。 – appl3r

0

你應該有一個RegisterInputModel和LoginInputModel按照那個來在默認的項目模板