Completing our Models

The last step is checking whether the input form valid or not. If the input is valid, we want to save the entry and display the entry's list page back to the user. So, we could just use conditional statement. Because of Entry class being a model in the Add.cshtml, we could use ModelState attribute.

[ActionName("Add"), HttpPost]
public ActionResult AddPost(Entry entry)
{
    if (ModelState.IsValid)
    {
        _entriesRepository.AddEntry(entry);
    }
    return View(entry);
}

Try to run the project.

Now, we want to standardize the Date variable. See here: https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings

<div class="form-group">
    @Html.LabelFor(m => m.Date, new { @class = "control-label"})
    @Html.TextBoxFor(m => m.Date, "{0:d}", new { @class = "form-control"})
</div>

Done.

Ideally, we would like to show an alert when user failed to input the form in valid term. We would to that in following chapter.

results matching ""

    No results matching ""