我在登录视图中有两个文本框和一个按钮(@ HTML.ActionLink),我想打开页面视图,其中视用户名和密码写在此文本框中我从数据库中获取相应的用户并将其传递到页面视图中以查看有关此用户的详细信息。
我有资源无法找到的问题。当这个问题出现在浏览器中时,链接会被修改(我认为问题就在这里):
http://localhost:50105/Users/LoginPage?Length=5
这是我的按钮:
@Html.ActionLink("Login", "LoginPage", "Users", new { @class = "btn btn-default", @style = "Color:red" })
这是我的 LoginPage 操作方法:
[HttpPost]
public ActionResult LoginPage([Bind(Include = "UserName,Password")] User user)
{
var per = db.Users.Where
(x => x.Password == user.Password && x.UserName == user.UserName)
.FirstOrDefault();
return View("Page",per);
}
这是我的页面查看:
@model MVCProject.Models.User
@{
ViewBag.Title = "Page";
}
<h2>Page</h2>
<div>
<h4>User</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.FirstName)
</dt>
<dd>
@Html.DisplayFor(model => model.FirstName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.LastName)
</dt>
<dd>
@Html.DisplayFor(model => model.LastName)
</dd>
</dl>
</div>
这是我的用户类:
public class User
{
public int UserID { get; set; }
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Password { get; set; }
}
这是 RouteConfig 类中的 RegisterRoutes 方法:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
这是我的登录视图:
@model MVCProject.Models.User
@{
ViewBag.Title = "Login";
}
</br>
<h2 align="Center">Login</h2>
<div>
</br>
</br>
</br>
<div align="center">
@Html.EditorFor(model => model.UserName, new { htmlAttributes = new
{ @class = "form-control", @placeHolder = "UserName" } })
@Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
</br>
@Html.EditorFor(model => model.Password, new { htmlAttributes = new {
@class = "form-control", @placeHolder = "Password", @type="password"} })
@Html.ValidationMessageFor(model => model.Password, "", new { @class =
"text-danger" })
</br>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10" align="center">
@Html.ActionLink("Login", "LoginPage", "Users", new { @class = "btn btn-default", @style = "Color:red" })
</div>
</div>
答案 0 :(得分:0)
您应该将控件放在表单中,然后创建一个提交类型的按钮,将模型数据发布到控制器。
@using (Html.BeginForm("LoginPage", "Users", FormMethod.Post))
{
<div align="center">
@Html.EditorFor(model => model.UserName, new { htmlAttributes = new
{ @class = "form-control", @placeHolder = "UserName" } })
@Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
</br>
@Html.EditorFor(model => model.Password, new { htmlAttributes = new {
@class = "form-control", @placeHolder = "Password", @type="password"} })
@Html.ValidationMessageFor(model => model.Password, "", new { @class =
"text-danger" })
</br>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10" align="center">
<input type="submit" value="Login" />
</div>
}