找不到该资源 MVC 5



>我有两个文本框和一个按钮(@HTML。ActionLink(在登录视图上,我想打开页面视图,根据此文本框中写入的用户名和密码,我从数据库中获取相应的用户,并在页面视图中传递它以查看有关此用户的详细信息。

我有找不到资源的问题。 当这个问题出现在浏览器中时,链接被修改为(我认为问题会在这里(:

http://localhost:50105/Users/LoginPage?Length=5

这是我的按钮:

@Html.ActionLink("Login", "LoginPage", "Users", new { @class = "btn btn-default", @style = "Color:red" })

这是我的登录页面操作方法:

[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>

您应该将控件放在窗体中,并使"提交"类型的按钮将模型数据发布到控制器。

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

最新更新