DOTNET:IMPORTING TEMPLATE AND REGISTRATION FORM WITH MVC ASP.NET

Step 1:Create a database table as follows,

CREATE TABLE [dbo].[Table] (
[Id] INT NOT NULL,
[profile] VARCHAR (50) NOT NULL,
[name] VARCHAR (50) NOT NULL,
[gender] VARCHAR (50) NOT NULL,
[dob] VARCHAR (50) NOT NULL,
[religion] VARCHAR (50) NOT NULL,
[contact] VARCHAR (50) NOT NULL,
[email] VARCHAR (50) NOT NULL,
[password] VARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);

TABLE

As the profile  and religion does need a dropdown ,we need to create a secondary table for that ctegory as follows,

CREATE TABLE [dbo].[profilelist] (
[Id] INT NOT NULL,
[Profile] VARCHAR (100) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);

TABLE_PROFILE

CREATE TABLE [dbo].[religionlist] (
[Id] INT NOT NULL,
[Religion] VARCHAR (50) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);

TABLE_RELIGION

Step 2:Now create a model for the tables as follows,

add_item

add_item1

add_item2

add_item3

Now the model looks like this,

add_item4

Step 3:Now create a view for the model.You can either generate a view after setting the controller or you can even use any HTML,Bootstrap  template for the view.

Incase if you are using any template,you need to follow this procedure,

Open your index.html from your  template,

  • Select the header and footer section from your index file and copy it in your /views/Shared/_Layout.cshtml.
  • Now copy the body section and paste it in /views/Home/index.cshtml enclosed in atag.
  • Now press ctrl+f(find) in both the index.cshtml file and search for src=” and replace it with src=”¬(tilt)/
  • Again search for href=” and replace it with href=”¬/.
  • Do the same for _Layout.cshtml file

find

Sorry that my tilt symbol isn’t working(:-

Inorder to connect it to the database model,you need to do some changes in the index file

The file should begin with

@model LOGINAPP.Table

LOGINAPP is my app name and Table is the database table name(its your model).

For example,my template looks like this,

template.png

I have a registration form with the fields that are similar to my database.

Now my job is to get the values from the fields and post it in my table.Inorder to do that i need to replace the form tag in  html file with

@using (Html.BeginForm("Index", "Home", FormMethod.Post))

Since you need to retrieve the values from textbox,dropdown etc,it is necessary to make changes in those syntax.

For example if you have a radiobutton with the following syntax in html,

[code language=”html”]


Gender:

–> [/code]

you need to change it as follows,

[code language=”html”]

Gender:

[/code]

Now we can retrieve the values from different fields and the controller posts the response to the database as follows,

Homecontroller.cs

public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
DbEntities db = new DbEntities();
ViewBag.ProfileLists1 = new SelectList(db.profilelists.ToList(), "Id", "Profile");    //This is to receive the values from secondary profile table .
ViewBag.ReligionLists1 = new SelectList(db.religionlists.ToList(), "Id", "Religion");//This is to receive the values from secondary religion table .(Dropdown)
return View();
}

//Code to post the values to the database.
[HttpPost]
public ActionResult Index(Table obj) 

{
if (ModelState.IsValid) {
DbEntities db = new DbEntities();   //Entity name
db.Tables.Add(obj);
db.SaveChanges();
}
return View(obj);
}

Note:You need to add the data to the profile and religion table inorder to fetch it in our model to categorise in dropdown list.

Thank you for reading!!

Leave a Reply

Your email address will not be published. Required fields are marked *