Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 27 - Customizing the autogenerated create view

Suggested Videos 
Part 24 - Deleting database records using post request in mvc
Part 25 - Insert update delete in mvc using entity framework
Part 26 - Customizing the autogenerated index view

In this video we will discuss, customizing the auto-generated create view. Please watch Part 26, before proceeding. 

At the moment, none of the fields on "Create" view are required. This means,when you click on the "Create" button without filling any data, NULL values are stored in all the columns of tblEmployee table.

So, how to make these fields on the "Create" view required?
Add [Required] attribute to the "Employee" class. The "Employee" class that is present in "EmployeeDataModel.Designer.cs" is auto-generated by the entity framework. There is no point in adding the [Required] attribute to this class, as we will loose the changes if the class is auto-generated again.

To achieve this, add a class file with "name=Employee.cs" to "Models" folder.

Copy and paste the following code in "Employee.cs" file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace MVCDemo.Models
    public partial class Employee

    public class EmployeeMetaData
        public string Name { get; set; }

        public string Gender { get; set; }

        public string City { get; set; }

        public int DepartmentId { get; set; }

At this point, run the application and click on the "Create" button without filling any data. Notice that we get validation error messages as expected. In a later video session, we will discuss changing the colour of the validation messages.

If you want "Select Department" as the first item in the "Department" dropdownlist on "Create" view, then, 
@Html.DropDownList("DepartmentId", String.Empty)

@Html.DropDownList("DepartmentId", "Select Department")

Notice that, a textbox is used for gender. It is ideal to have a dropdownlist for gender rather than a textbox. To achieve this, make the following changes to "Create.cshtml" view.

@Html.EditorFor(model => model.Gender)

@Html.DropDownList("Gender", new List<SelectListItem>
new SelectListItem { Text = "Male", Value="Male" },
new SelectListItem { Text = "Female", Value="Female" }
}, "Select Gender")


  1. hello i have been following this tutorials and I created the Entity Model and automatically generated the views as well.

    In the Create view, the department names are not shown as a dropdown menu but as an @HTML.EditFor(). I tried doing @Html.DropDownList("DepartmentId", "Select Department") in the view but i keep getting an error saying there is no ViewData item of type IEnumerable that has the key 'DepartmentId'

    I tried using ViewBag.Departments = new SelectList(db.Departments, "Id", "Name"); which works in creating the dropdown list but when i submit the form, the departmentId is null and it inputs null into the db. All other fields of the Create view works normally apart from this. Please help. Thank you

    1. I think you are having issue with the Key/Column/Property name mismatch anywhere in the code. Make Sure you are having the same name for all the fields/properties as DepartmentId for that mentioned field including the auto-generated classes too...

      @Html.DropDownList("DepartmentId", "Select Department")
      @Html.ValidationMessageFor(model => model.DepartmentId)

  2. Hi Venkat,
    I have a doubt regarding data binding to the Department dropdown list in Create view.
    we used the following statement for department
    @Html.DropDownList("DepartmentId", "Select Department")
    but we didn't provide any data in the form of a list like the gender dropdown.
    Can you please how this binding is happening automatically.

  3. [required] function is not working in partial class...why???

  4. Hi, i have created simple project and added a new table with name student with entity frame work, and all CRUD funtionalities are working with entity frame but when i tried to apply partial class validation, then is post the data to the controller not validating the partial class. can you tell me what could be the problem ?


If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.