c# - Object Reference not set to an object (calling Razor model from View) -
this question has answer here:
- what nullreferenceexception, , how fix it? 29 answers
using c# mvc4
my view:
@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>
my model:
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, keep getting error:
when try debug it, doesn't go html.textbox
method or model.
without seeing controller action, guess model null.
in controller, make sure passing instance of model view. example:
return view(new usermodel());
instead of:
return view();
Comments
Post a Comment