MVC Interview Question - 4

What is HTML Helpers in MVC

The ASP.NET MVC framework includes helper methods that provide an easy way to render HTML in a view.


Below render CheckBox in MVC Application

<%= Html.CheckBox("bookType") %>

TextBox Helper

Enter your name: <%= Html.TextBox("name") %>

List out the HTML Helpers Available in MVC


ActionLink — Links to an action method.

BeginForm * — Marks the start of a form and links to the action method that renders the form.
CheckBox * — Renders a check box.
DropDownList * — Renders a drop-down list.
Hidden — Embeds information in the form that is not rendered for the user to see.
ListBox — Renders a list box.
Password — Renders a text box for entering a password.
RadioButton * — Renders a radio button.
TextArea — Renders a text area (multi-line text box).
TextBox * — Renders a text box

What is the AJAX Helpers in ASP.Net


AJAX Helpers are used to create AJAX enabled elements like as Ajax enabled forms and links which performs request asynchronously. AJAX Helpers are extension methods of AJAXHelper class which exist in System.Web.Mvc namespace.


Following example shows how to use AJAX action link using action and controller in Asp.Net MVC.


@Ajax.ActionLink("Fatch Data", "GetData", new AjaxOptions {UpdateTargetId = "Data-container", HttpMethod = "GET" })


Here is the html output of above code block.


<a data-ajax="true" data-ajax-method="GET" data-ajax-mode="replace" data-ajax-update="#Data-container" href="/Home/GetData"> Fatch Data </a>




What is JSON Binding in MVC?

JavaScript Object Notation (JSON) binding support started from ASP.Net MVC3 onwards via the new JsonValueProviderFactory, which allows the action methods to accept and model-bind data in JSON format. This is useful in Ajax scenarios like client templates and data binding that need to post data back to the server.


What is Dependency Resolution in MVC?

Dependency Resolver again has been introduced in ASP.Net MVC3 and it is greatly simplified the use of dependency injection in your applications. This turn to be easier and useful for decoupling the application components and making them easier to test and more configurable.


Explain Bundle.Config in ASP.Net MVC4?

"BundleConfig.cs" in ASP.Net MVC4 is used to register the bundles by the bundling and minification system. Many bundles are added by default including jQuery libraries like - jquery.validate, Modernizr, and default CSS references.

What is the difference between View and Partial View



View Partial View
It contains the layout page It does not contain the layout page
Before any view is rendered, viewstart page is rendered Partial view does not verify for a viewstart.cshtml. We cannot put common code for a partial view within the viewStart.cshtml.page
View might have markup tags like body, html, head, title, meta etc. Partial view is designed specially to render within the view and just because of that it does not consist any mark up
View is not lightweight as compare to Partial View We can pass a regular view to the RenderPartial method


How to perform validation in MVC ?

one of the way to do validation in MVC using data annotations.
it is attributes whih can be applied to model peoperties.

See the below example.





public class Customer

{

    [Required(ErrorMessage="Customer code is required")]

    public string CustomerCode

    {

        set;

        get;

    } 

}  



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("PostCustomer", "Home", FormMethod.Post))

{ %>

<%=Html.TextBoxFor(m => m.CustomerCode)%>

<%=Html.ValidationMessageFor(m => m.CustomerCode)%>

<input type="submit" value="Submit customer data" />

<%}%> 




How can we enable data annotation validation on client side?

It’s a two-step process: 
first reference the necessary jQuery files.



<script src="<%= Url.Content("~/Scripts/jquery-1.5.1.js") %>" type="text/javascript"></script>



The second step is to call the EnableClientValidation method.








<% Html.EnableClientValidation(); %> 



How to use Jquery Plugins in ASP.Net MVC validation?

We can use dataannotations for validation in ASP.Net MVC. If we want to use validation during runtime using Jquery then we can use Jquery plugins for validation. Eg: If validation is to be done on customer name textbox then we can do as :




$('#CustomerName').rules("add", {

required: true,

minlength: 2,

messages: {

required: "Please enter name",

minlength: "Minimum length is 2"

}



});






MVC Interview Question                          MVC Interview Question - 1




Posted in Labels: , |

0 comments:

Post a Comment