How to design view (.cshtml) in MVC 4.0 and use different controls into it?

This article will demonstrate you about how to design view (.cshtml) page and ways to put different controls in it. There are many ways to achieve required functionality using HTML5, JQuery etc. For master page, in MVC4.0, you have to use Layout.cshtml. You can add content pages as different views.

1.Lets say, you have added view index.cshtml then you will find

ViewBag.Title = "Name of controller or any name you can give here.";

So ViewBag.Title is nothing but which title you want to give.
2. At top @model you have to mention which model you are using. If you want to iterate data using foreach property then you have to use IEnumerable.

@model name of model

3. If you want to post values entered by user on the screen then you have to use following :

@using (Html.BeginForm("Actionname", "Controller name", FormMethod.Post, new { enctype = "multipart/form-data" }))
//Put all controls inside this.

I have used above code for file upload control so that my posted filename can be send through this. Whenever you want button click event, you can use this code.
How to use file upload control?

<input type="file" id="MyFileUploadControl" name="MyFileUploadControl" / >

4. How to use Label control?

@Html.LabelFor(m => m.yourPropertyName, "Property1")

Here I have used lambda expression.
5. How to use dropdown control?

@Html.DropDownListFor(m=>m.yourPropertyName , (IEnumerable<SelectListItem>) ViewBag.namegivenbyDeveloper)

6.How to use validation message for any control?



<div class="editor-field">

7.How to use checkbox?



No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: