提问者:小点点

对象引用未设置为对象(从视图调用剃刀模型)[重复]


使用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;
    }
}

但是,我不断地得到错误:

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


共1个答案

匿名用户

如果没有看到您的控制器操作,我猜您的模型是空的。

在您的控制器中,确保您正在将模型的实例传递给您的视图。例如:

return View(new UserModel());

而不是:

return View();