首页 > 解决方案 > 怎么修 ”.Model.get 在 MVC 应用程序中返回 null”?

问题描述

我正在使用登录视图执行此应用程序,并且我有一个用户模型,其中包含用户名和密码以在登录时填写(如常规)。我在视图中的 using 语句中收到此错误:

System.Web.Mvc.WebViewPage<TModel>.Model.get returned null.

在我实现这个之前,但我改变了它,因为我使用的是一个使用输入和跨度而不是 labelfor 和 editfor 的示例模板:

@using (Html.BeginForm("Authorise", "Login", FormMethod.Post))
        {
            <table>
                <tr>
                    <td></td>
                    <td style="text-decoration:underline"></td>
                </tr>
                <tr>
                    <td>@Html.LabelFor(model => model.Username)</td>
                    <td>@Html.EditorFor(model => model.Username)</td>
                </tr>
                <tr>
                    <td>@Html.ValidationMessageFor(model => model.Username)</td>
                    <td></td>
                </tr>
                <tr>
                    <td>@Html.LabelFor(model => model.Password)</td>
                    <td>@Html.EditorFor(model => model.Password)</td>
                </tr>
                <tr>
                    <td></td>
                    <td>@Html.ValidationMessageFor(model => model.Password)</td>
                </tr>
                <tr>
                    <td colspan="2">
                        <label class="field-validation-error">@Html.DisplayFor(model => model.LoginMessage)</label>
                    </td>
                </tr>
                <tr>
                    <td><input type="submit" value="Login" /></td>
                    <td><input type="reset" value="Clean" /></td>
                </tr>
            </table>
        }

在更改之前,一切正常。我将视图更改为:

@using (Html.BeginForm("Authorise", "Login", FormMethod.Post))
            {
                <form class="login100-form validate-form">
                    <span class="login100-form-logo">
                        <i class="zmdi zmdi-landscape"></i>
                    </span>

                    <span class="login100-form-title p-b-34 p-t-27">
                        Log in
                    </span>

                    <div class="wrap-input100 validate-input" data-validate="Enter username">

                        <input class="input100" type="text" name="Username" placeholder="Username" value="@Model.Username">
                        <span class="focus-input100" data-placeholder="&#xf207;"></span>
                    </div>

                    <div class="wrap-input100 validate-input" data-validate="Enter password">
                        <input class="input100" type="password" name="Password" placeholder="Password" value="@Model.Password">
                        <span class="focus-input100" data-placeholder="&#xf191;"></span>
                    </div>

                    <div class="contact100-form-checkbox">
                        <input class="input-checkbox100" id="ckb1" type="checkbox" name="remember-me">
                        <label class="label-checkbox100" for="ckb1">
                            Remember me
                        </label>
                    </div>

                    <div class="container-login100-form-btn">
                        <button class="login100-form-btn">
                            Login
                        </button>
                    </div>

                    <div class="text-center p-t-90">
                        <a class="txt1" href="#">
                            Forgot Password?
                        </a>
                    </div>
                </form>
            }

控制器:

public ActionResult Index()
    {
        using (FITMEContext db = new FITMEContext())
        {

        }

        return View();
    }

    //For login
    [HttpPost]
    public ActionResult Authorise(User user)
    {
        using (FITMEContext db = new FITMEContext())
        {
            var userDetail = db.Users.Where(x => x.Username == user.Username && x.Password == user.Password).FirstOrDefault();

            if (userDetail == null)
            {
                user.LoginMessage = "Invalid username or password";
                return View("Index", user);
            }
            else
            {
                Session["UserId"] = user.UserId;
                Session["UserName"] = user.Username;
                return RedirectToAction("Index", "Home");
            }
        }            
    }

模型:

public partial class User
{
    public int UserId { get; set; }

    [DisplayName("User name")]
    [Required(ErrorMessage = "Please fill in the user name")]
    public string Username { get; set; }

    [Required(ErrorMessage = "Please fill in the password")]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    public string LoginMessage { get; set; }
}

任何帮助,将不胜感激。谢谢。

标签: c#razormodel-view-controller

解决方案


在“索引”方法中,您调用 View() 时不带参数。这就是为什么 Model == null

一种可能的解决方案是创建默认模型对象并将其传递给视图:

return View("Index", new User())

推荐阅读