【问题标题】:How to authorise user from the forum's main page?如何从论坛主页授权用户?
【发布时间】:2011-09-22 23:15:32
【问题描述】:

我正在尝试实现一个包含主题的简单博客

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

}

主页看起来像一个主题列表。

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;

   }

}

主页的视图很简单。

观看次数/首页/索引

@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”控制器,将用户的登录名和密码传递给它。 “帐户”控制器应验证此用户并将浏览器重定向到带有主题的主页。

但我无法访问视图中的登录名和密码编辑框...而且我真的不知道该怎么办,我的模型是否正确

【问题讨论】:

    标签: asp.net-mvc-3 razor


    【解决方案1】:

    如果您希望在提交表单时将它们的值发布到服务器,这些输入字段应该在您的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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-30
      相关资源
      最近更新 更多