首页 > 解决方案 > 找不到资源 MVC 5

问题描述

我在登录视图上有两个文本框和一个按钮(@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>

标签: c#asp.netasp.net-mvc

解决方案


你应该把你的控件放在一个表单中,并制作一个提交类型的按钮来将你的模型数据发布到控制器。

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

推荐阅读