Previous Question

Question 81

Next Question

2.2.1 Question

Multiple Choice

You are creating an ASP.NET MVC web application. The application must accept user input for a ProductName field. To reduce delays due to invalid entries making round trips between the client and server, user input should be validated on the client before being submitted to the server. Which code segment should you choose?

  • A. <div class="editor-label">@Html.LabelFor(model => model.ProductName)</div>
    <div class="editor-field">@Html.EditorFor(model => model.ProductName)</div>
  • B. <div class="editor-label">@Html.LabelFor(model => model.ProductName)</div>
    <div class="editor-field">@Html.ValidationMessageFor (model => model.ProductName)</div>
  • C. <div class="editor-field">
    @Html.EditorFor(model => model.ProductName)
    @Html.ValidationMessageFor(model => model.ProductName)
    </div>
  • D. <div class="editor-label">@Html.LabelFor(model => model.ProductName)</div>

Answer:

C
Explanation
A. Incorrect: @Html.EditorFor only inserts data. The data is verified on the server side.
B. Incorrect: @Html.ValidationMessageFor displays the validation message. There is no code included in this answer choice to validate the data.
C. Correct: @Html.EditorFor, in combination with @Html.ValidationMessageFor, are used for client-side validation.
D. Incorrect: @Html.LabelFor only displays labels for the items.