如何在MVC应用程序中修复“ <TModel> .Model.get返回null”?

时间:2019-09-09 19:52:25

标签: c# razor model-view-controller

我正在使用登录视图执行此应用,并且我有一个用户模型,该模型具有要在登录时填写的用户名和密码(按常规)。我在视图的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; }
}

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

3 个答案:

答案 0 :(得分:3)

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

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

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

答案 1 :(得分:2)

辅助方法@Html.EditorFor(model => model.Username)在尝试访问Model之前会一直检查是否为null。现在,您自己呈现值,而不是使用辅助方法,则需要显式添加空检查。

<input class="input100" type="text" name="Username" placeholder="Username" value="@(Model == null ? "" : Model.Username)">

作为替代方案,您仍然可以使用辅助方法并指定输入的类:

@Html.EditorFor(model => model.Username, new { @class = "input100" })

答案 2 :(得分:0)

我现在已更改为以下内容,但是“提交”按钮仅刷新了我不知道为什么的页面。它正在其他布局中工作。有什么建议吗?似乎很容易解决

<div class="wrap-login100">
            <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 == null ? "" : 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="text" name="Username" placeholder="Username" value="@(Model == null ? "" : 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>*@

                <input type="submit" value="Login" />

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