I have an Object, say Employee with the following:
Employee() { int id; string name; } In my view,
I currently do
<input type="text" name="employeeName" id="employeeName" readonly="true" value="@Model.Employee.name"/> However, in certain cases the Employee value will be null and this will give me an error, null reference error. Should I do inline checks to return an empty string if it's null or is there a better way?
would the @HTML.TextboxFor() method work better in this case?
name="employeeName"which has no relationship to you model and will not be bound on post back (your model does not have a property namedemployeeName)? Use@Html.TextBoxFor(m => m.name, new { readonly = "readonly" })DisplayFormat.NullDisplayTextis only respected by@Html.DisplayFor()(not byTextForFor()orEditorFor())