对象引用未设置为对象(从View调用Razor模型)
使用C#MVC4
我的看法:
@using Universe.Models @model UserModel @section css { } @using (Html.BeginForm("AddUser","Home", FormMethod.Post)) { Alias: | @Html.TextBox(Model.Alias) | |
我的型号:
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
方法或我的模型。
没有看到你的控制器动作,我的猜测是你的模型为空。
在控制器中,确保将模型实例传递给视图。 例如:
return View(new UserModel());
代替:
return View();
返回特定View
时,必须在Controller
Action中传递Model
return View(new Model());