13. What is Partial View in ASP.NET MVC?Answer:
Partial view is a reusable view (like user control). A partial view is a view that is rendered within another view which means it can be embedded inside other view.
Partial view can be rendered using Html.Partial(), Html.RenderPartial(), Html.RenderAction() method.
14. Explain the difference between View and Partial View?Answer:
- View contains the layout page.
- Before any view is rendered, viewstart page is rendered.
- It is not lightweight as compare to Partial View.
- View might have markup tag like html, head, title, body etc.
- Partial view does not contain layout page.
- We can pass regular view to the render partial method.
- Partial view is designed specially to render within the view and just because of that it does not consist any mark up.
- Partial View is lightweight compared to View.
- Partial view can be embedded inside another view.
15 What are the HTML helpers in ASP.NET MVC?Answer:
Html helpers are a method that returns a HTML string. HTML helpers help us to render HTML controls in the view. HTML helper does not have an event model and a view state.
We can also create our own HTML Helpers to render more complex content.
There are three types of HTML helpers.
Inline HTML helpers
Built-in HTML helpers
- Standard HTML helpers
- Strongly type HTML helpers
- Templated HTML Helpers
Custom HTML helper
If we want to add HTML textbox on view, below is the HTML helper.
<%= Html.TextBox("Name") %>
16. What is the difference between "HTML.TextBox" and "HTML.TextBoxFor"?Answer:
HTML.TextBoxFor is strongly type while HTML.TextBox is not, but they both produce same HTML.
Html.TextBoxFor(m => m.Name)
will both produce
<input id="Name" name="Name" type="text" />
Using the typed "TextBoxFor" version allows using compile time checking. So if we change our model then we can check whether there are any errors in your views.
17. How can we do validation in ASP.NET MVC?Answer:
ASP.NET MVC uses DataAnnotations attributes to implement validations. It is one of the easiest ways to do validation in MVC.
For example, in the code snippet below we have a simple Employee class with a property EmpCode.
This EmpCode property is tagged with a Required data annotation attribute.
public class Employee
[Required(ErrorMessage="Emp code is required")]
public string EmpCode
In order to display the validation error message we need to use the ValidateMessageFor method which belongs to the Html helper class.
<% using (Html.BeginForm("PostEmployee", "Home", FormMethod.Post))
<%=Html.TextBoxFor(m => m.EmpCode)%>
<%=Html.ValidationMessageFor(m => m.EmpCode)%>
<input type="submit" value="Submit Employee data" />
Later in the controller we can check if the model is proper or not by using the ModelState.IsValid property and accordingly we can take actions.
public ActionResult PostEmployee(Employee obj)
18. What is validation summary?Answer:
The validation summary is used to display all the error messages in the view. It displays an unordered list of all validation errors in the ModelState dictionary object.
@Html.ValidationSummary(true) @*//shows model-level errors*@
@Html.ValidationSummary(false) @*//shows model-level and property-level errors*@