2011-09-22 28 views
0

我想實現一個簡單的博客,其中包含主題如何從論壇主頁授權用戶?

型號/ Topic.cs

public class Topic 
{ 
    public int ID {get; set;} 

    [StringLength(50)] 
    [RequiredAttribute(ErrorMessage = "*")] 
    public string Title {get; set;} 

    [StringLength(1024)] 
    [RequiredAttribute(ErrorMessage = "*")] 
    public string Body { get; set; } 

    public int CommentsCount { get; set; } 

    public DateTime TimeLastUpdated { get; set; } 
    public int AuthorID { get; set; } 

    public virtual List<Comment> commentsList { get; set; } 

} 

主頁看起來像的主題列表。

控制器/ HomeController.cs

public class HomeController : Controller 

    private ContentStorage db = new ContentStorage(); 
    public ViewResult Index() 
    { 
     // Topics = DbSet<Topic> Topics { get; set; } 
     return View(db.Topics.ToList()); 
    } 

    [HttpPost] 
    public void LogIn(string login, string password) 
    { 
     int i; 
     i = 10; 

    } 

} 

主頁的觀點是非常簡單的。

查看/首頁/指數

@model IEnumerable<MvcSimpleBlog.Models.Topic> 
... 
<table width="95%" height="86" border="0"> 
     <tr> 
     <td width="45%" valign = "bottom" >Login:</td> 
     <td width="45%" valign = "bottom" >Password:</td> 
     <td width="10%"></td> 
     </tr> 

     <tr> 
     <td width="45%"><p> <input type="text" name="login" /> </p></td> 
     <td width="45%"><p><input type="password" name="password" /></p></td> 
     <td width="10%" align = "left"> 
      @using (Html.BeginForm("LogIn", "Home")) 
      { 
       <input type = "submit" value = "Enter" /> 
      } 
     </td> 
     </tr> 

     <tr> 
     <td width="45%" valign = "top" >@Html.ActionLink("Register", "Register", "Account")</td> 
     </tr> 

</table> 

我怎麼能忽略在視圖中HomeController的方法從編輯框的值? 「LogIn」方法應該從視圖中接收數據,調用「Account」控制器,並將用戶的登錄名和密碼傳遞給它。 「帳戶」控制器應驗證此用戶並將瀏覽器重定向到包含主題的主頁面。

但我不能訪問帳號和密碼編輯框的觀點...我真的不知道我該怎麼辦,是我的模型正確

回答

0

這些輸入字段應該是裏面的Html.BeginForm如果您希望在提交表單時將其值發佈到服務器。目前,您的表單中只有一個提交按鈕。所以:

@using (Html.BeginForm("LogIn", "Home")) 
{ 
    <table width="95%" height="86" border="0"> 
     <tr> 
      <td width="45%" valign="bottom">Login:</td> 
      <td width="45%" valign="bottom">Password:</td> 
      <td width="10%"></td> 
     </tr> 
     <tr> 
      <td width="45%"> 
       <p> 
        <input type="text" name="login" /> 
       </p> 
      </td> 
      <td width="45%"> 
       <p> 
        <input type="password" name="password" /> 
       </p> 
      </td> 
      <td width="10%" align="left"> 
       <input type="submit" value="Enter" /> 
      </td> 
     </tr> 
     <tr> 
      <td width="45%" valign="top"> 
       @Html.ActionLink("Register", "Register", "Account") 
      </td> 
     </tr> 
    </table> 
} 

現在你LogIn控制器動作可以收到2倍的值作爲動作參數,像這樣:

[HttpPost] 
public ActionResult LogIn(string login, string password) 
{ 
    ... perform authentication 
}