k-World

Archive for the ‘MS – MVC’ Category

To make a text box read only in ASP.Net MVC 4 (Razor View)

The different ways how we can make a text box read only as mentioned below:

1. Using the read-only attribute in the TextBox HtmlHelper, i.e.

@Html.TextBoxFor(Model => Model.Customer, new { @readonly = “readonly” })

2. Also by setting the read-only attribute to true in the TextBox HtmlHelper, i.e.

@Html.TextBoxFor(Model => Model.Customer, new { @readonly = true })

Reference:

http://kreotekdev.wordpress.com/2007/11/08/disabled-vs-readonly-form-fields/

Key Features of MVC

Mentioning below some of the essential features of MVC-4 which you required when working on MVC projects,

  • Routing – To handle the incoming and outgoing URL Requests.
  •  Controllers & Action Methods – Each request is handled by the specific controller and the action method depending upon which a specific view is rendered.
  • Views – The HTML part which is rendered by the action method.
  • Filters – Helps to add the extra logic to request processing Ex: Authentication, Action, Result and Exception filters.

AARE i.e A2RE which is easy to remember the types of filters at the time of interview.

  • Helper Methods – Which help to write the HTML code in the View, and can be reused in the application.
  • Model Binding – Helps in creating the .NET objects using the data sent by the browser in the HTTP request.
  • Model Validation – Helps to fetch the valid data without any errors.
  • Web API – Web services which provide API to HTTP clients (GET, POST, PUT etc).
  • Bundling and Minification – Helps in performance optimization along with the java script files minification.
  • On Demand Loading (using AJAX/jQuery GET and POST types) – Help to load only the specific data only when required which in turn help in application performance.
  • Partial Views (Reusability) – which is similar to the YourUser.ascx user controls in ASP.NET.
  • Layout – similar to the Master Page.
  • Sections – helps to provide regions of content within the layout.
  • Child Actions – similar to the partial view but written in the controller.
  • Razor – View Engine (Syntax is easy as compared to the ASPX View Engine).
  • View Data – Help to maintain data when we move from the controller to view.
  • View Bag – Dynamic wrapper around View Data. Contains lists of View Data.
  • Temp Data – Helps to move data from one controller to other.
  • Action Result  & View Result – View Result is derived from the Action Result.
  • RESTful Services – Using the WEB API.
  • TDD – Helps in following Test Driven Development.
  • Seperation Of Concern and Loose Coupling (with help of interfaces).

Reference: Pro ASP.NET MVC 4 – Fourth Edition

Basics of MVC

M V C – Model View Controller

Its an presentational design pattern which implements the separation of concern concept, means all the UI (User Interface) layer, Business layer and Database layers are separated/decoupled . Which helps to maintain the code clean and tidy.

Model provides the real world data object. Which provides the data to the View from the data/entity/domain object.

View represent the HTML which needs to be rendered in the web browsers. View renders just the data obtained from the controller.

Controller is the core of MVC. Which takes care of the data which needs to be sent to the view and in turn is re-directed to view which will be rendered in the HTML.

General flow of MVC, when a user requests the data. The request will first hit the controller(action method), the controller will in-turn speaks to the Model and after processing the data. The processed data is sent to the View from the Controller. Finally view will be rendered accordingly.

Different Versions of MVC in asp.net till date:MVC 1 » MVC 2 » MVC 3 » MVC 4….