开发者

How to authorise user from the forum's main page?

I'm trying to implement a simple blog, which contains topics

models/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; }

}

Main Page looks like a list of topics.

Controllers/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;

   }

}

Main page's view is very simple.

Views/Home/Index

@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%"&g开发者_C百科t;<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>

How could i pass the values from edit boxes in the View to the HomeController method? The method "LogIn" was supposed to receive data from the view, call the "Account" controller, passing user's login and password to it. An "Account" controller shoud validate this user and redirect browser to the main page with topics.

But i can't access login and password edit boxes in the view... and i really don't know what should i do, and is my model correct


Those input fields should be inside your Html.BeginForm if you want their values to be posted to the server when the form is submitted. Currently you only have a single submit button inside your form. So:

@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>
}

Now your LogIn controller action could receive the 2 values as action parameters like so:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜