对象未设置为一个对象(调用从View剃刀模型) [英] Object Reference not set to an object (calling Razor model from View)

查看:146
本文介绍了对象未设置为一个对象(调用从View剃刀模型)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#MVC4

我的观点:

@using Universe.Models
@model UserModel
@section css {
<link href="@Url.Content("~/Content/assets/charcreation.css")" rel="stylesheet"/>}
@using (Html.BeginForm("AddUser","Home", FormMethod.Post))
{

<div class="row-fluid">
            <table id="tblBio">
                <tr>
                    <td class="span3">
                        <span class="labeltext">Alias:</span>
                    </td>
                    <td class="span5">
                        @Html.TextBox(Model.Alias)
                    </td>
                    <td class="span4">
                        <span class="ui-state-highlight hidden"></span>
                    </td>
                </tr>

我的模型:

public class UserModel
{
    public int Id { get; set; }
    public string Alias { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public bool IsExternal { get; set; }


    public UserModel()
    {

    }

    public UserModel(User user)
    {
        if (user == null) return;
        Alias = user.Alias;
    }
}

不过,我不断收到错误:

But, I keep getting the error:

当我尝试调试它,它甚至没有进入 Html.TextBox 方法或进入我的模型。

When I try to debug it, it doesn't even go into the Html.TextBox method or into my model.

推荐答案

在没有看到你的控制器动作,我的猜测是,你的模型为null。

Without seeing your controller action, my guess would be that your model is null.

在你的控制器,请确保您传递模型的实例,你的看法。例如:

In your controller, make sure you are passing an instance of the model to your view. For example:

return View(new UserModel());

而不是:

return View();

这篇关于对象未设置为一个对象(调用从View剃刀模型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆