0

I asked a question yesterday about how to fix a sudden and unexpected "Object not set to an instance of an object" error. The only real answer I got was to pass a new instance of the object to the view like this:

// GET: WC_Inbox/Create
public ActionResult Create(int? id)
{
    System.Diagnostics.Debug.WriteLine("Employee ID was: " + id);
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Employee employee = db.Employees.Find(id);
    if (employee == null)
    {
        return HttpNotFound();
    }
    string fullName = employee.First_Name + " " + employee.Last_Name;
    System.Diagnostics.Debug.WriteLine("Employee full name: " + fullName);
    ViewBag.EmployeeID = id;
    ViewBag.Name = fullName;
    ViewBag.Status = "Pending";
    return View(new WC_Inbox());
}

With the primary line in question being here: return View(new WC_Inbox());

This worked, however it has created further issues. Now on my create page I am given some incorrect data in some fields

bad

The fields Org Number, Hire Date, Work Schedule, and Injury date should not show those values. Those are incorrect and will be very confusing to the mostly elderly client bass the app is intended for. I would like for the placeholder values to show up there rather than this incorrect data.

This is the code from the Create view for those fields:

<div class="form-group">
        @Html.LabelFor(model => model.Org_Number, "Org Number", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Org_Number, new { htmlAttributes = new { @class = "form-control", placeholder = "0025" } })
            @Html.ValidationMessageFor(model => model.Org_Number, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Hire_Date, "Hire Date", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Hire_Date, new { htmlAttributes = new { @class = "form-control", placeholder = "8/3/2021" } })
            @Html.ValidationMessageFor(model => model.Hire_Date, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Job_Title, "Job Title", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Job_Title, new { htmlAttributes = new { @class = "form-control", placeholder = "Programmer Analyst" } })
            @Html.ValidationMessageFor(model => model.Job_Title, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Work_Schedule, "Work Schedule", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Work_Schedule, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Work_Schedule, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Injury_Date, "Injury Date", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Injury_Date, new { htmlAttributes = new { @class = "form-control", placeholder = "8/14/2021" } })
            @Html.ValidationMessageFor(model => model.Injury_Date, "", new { @class = "text-danger" })
        </div>
    </div>

How can I get rid of that bad data and use the placeholder values I assigned instead?

PS: Work Schedule should simply be blank. There is no placeholder value.

3
  • It seems like your pass an empty WC_Inbox object to the view. It is necessary to populate it with some data before return View(...); Commented Aug 3, 2021 at 16:36
  • Why would I populate it with data before sending it to the view? All of those fields need to be null when the view is rendered. If I were to populate it I would simply populate every value with null. That doesn't seem like a very elegant way to do this... Commented Aug 3, 2021 at 16:39
  • You wrote “Now on my create page I am given some incorrect data in some fields”. This is not “incorrect data”, this is a default values for according to the type. Probably you should title your question as “Displaying a default type values instead of ploceholder's values”? This will more accurately reflect your problem. Commented Aug 3, 2021 at 16:48

1 Answer 1

2

The issue is that you are passing a default object to the strongly-typed view. On the line:

return View(new WC_Inbox());

you have not done anything to initialize the properties/values on the WC_Inbox object. This means that all the values will be the default value (0 for int, null for string, the minimum date time for DateTime, etc.) It is those default values that you are seeing on your view. If you would like blank default values, you just have to update the WC_Inbox class to support nullable types. This works because null values display as blanks, so your placeholder text will show up. For example, it worked as expected for Job_Title since Job_Title is a string and its default value is null which is then displayed as a blank by EditorFor which allows the placeholder text to display. Hopefully that makes sense - it is simple, but verbose to explain.

If your WC_Inbox class can allow for nullable values, update it and then your placeholders would work. So the class now looks like:

class WC_Inbox
{
    public int? Org_Number {get; set;}
    public DateTime? Hire_Date { get; set;}
    public string Job_Title {get; set;} // string is nullable already
    public DateTime? Work_Schedule {get; set;}
    public DateTime? Injury_Date {get; set;}
}

If you update the definition of WC_Inbox then the code as you have it will work.

Sign up to request clarification or add additional context in comments.

1 Comment

Really good answer. Thank you for this. Learned some things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.