Here’s an example of how to create a page registration form in ASP.NET MVC using a stored procedure where the table fields are “EmployeeID, FirstName, LastName, Email, Phone, HireDate, Department, Salary”:
Create Stored Procedure
First, create a stored procedure that handles the employee registration:
sql code
CREATE PROCEDURE [dbo].[usp_EmployeeRegistration]
@EmployeeID INT OUTPUT,
@FirstName VARCHAR(50),
@LastName VARCHAR(50),
@Email VARCHAR(50),
@Phone VARCHAR(20),
@HireDate DATE,
@Department VARCHAR(50),
@Salary DECIMAL(10,2)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO Employee (FirstName, LastName, Email, Phone, HireDate, Department, Salary)
VALUES (@FirstName, @LastName, @Email, @Phone, @HireDate, @Department, @Salary)
SET @EmployeeID = SCOPE_IDENTITY();
END
The produced EmployeeID is returned by this stored method, which accepts parameters for each employee field.
Create Model
Create a model class for the Employee table:
csharp
public class Employee
{
public int EmployeeID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public DateTime HireDate { get; set; }
public string Department { get; set; }
public decimal Salary { get; set; }
}
Create Controller
In the controller, create an action for the registration form:
csharp
public class EmployeeController : Controller
{
private string connectionString = “YourConnectionString”;
public ActionResult Register()
{
return View();
}
[HttpPost]
public ActionResult Register(Employee employee)
{
if (ModelState.IsValid)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(“usp_EmployeeRegistration”, connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue(“@FirstName”, employee.FirstName);
command.Parameters.AddWithValue(“@LastName”, employee.LastName);
command.Parameters.AddWithValue(“@Email”, employee.Email);
command.Parameters.AddWithValue(“@Phone”, employee.Phone);
command.Parameters.AddWithValue(“@HireDate”, employee.HireDate);
command.Parameters.AddWithValue(“@Department”, employee.Department);
command.Parameters.AddWithValue(“@Salary”, employee.Salary);
command.Parameters.Add(“@EmployeeID”, SqlDbType.Int).Direction = ParameterDirection.Output;
connection.Open();
command.ExecuteNonQuery();
int employeeId = (int)command.Parameters[“@EmployeeID”].Value;
}
return RedirectToAction(“Index”);
}
return View(employee);
}
}
The registration form is displayed by this controller through an action, and the form submission is handled by a post action. To enter employee data and obtain the created EmployeeID, it makes use of the stored process.
Create View
Create a view for the registration form:
xml
@model Employee
@{
ViewBag.Title = “Employee Registration”;
}
<h2>Employee Registration</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class=”form-group”>
@Html.LabelFor(model => model.FirstName)
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = “form-control” } })
@Html.ValidationMessageFor(model => model.FirstName, “”, new { @class = “text-danger” })
</div>
<div class=”form-group”>
@Html.LabelFor(model => model.LastName)
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = “form-control” } })
@Html.ValidationMessageFor(model => model.LastName, “”, new { @class = “text-danger” })
</div>
<div class=”form-group”>
@Html.LabelFor(model => model.Email)
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = “form-control” } })
@Html.ValidationMessageFor(model => model.Email, “”, new { @class = “text-danger” })
</div>
<div class=”form-group”>
@Html.LabelFor(model => model.Phone)
@Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = “form-control” } })
@Html.ValidationMessageFor(model => model.Phone, “”, new { @class = “text-danger” })
</div>
<div class=”form-group”>
@Html.LabelFor(model => model.HireDate)
@Html.EditorFor(model => model.HireDate, new { htmlAttributes = new { @class = “form-control” } })
@Html.ValidationMessageFor(model => model.HireDate, “”, new { @class = “text-danger” })
</div>
<div class=”form-group”>
@Html.LabelFor(model => model.Department)
@Html.EditorFor(model => model.Department, new { htmlAttributes = new { @class = “form-control” } })
@Html.ValidationMessageFor(model => model.Department, “”, new { @class = “text-danger” })
</div>
<div class=”form-group”>
@Html.LabelFor(model => model.Salary)
@Html.EditorFor(model => model.Salary, new { htmlAttributes = new { @class = “form-control” } })
@Html.ValidationMessageFor(model => model.Salary, “”, new { @class = “text-danger” })
</div>
<div class=”form-group”>
<input type=”submit” value=”Register” class=”btn btn-primary” />
</div>
}
This view creates the registration form using the Html.BeginForm helper and shows the form fields. For every field, there are validation messages as well.
You may use a stored procedure with the given table fields to generate a page registration form in ASP.NET MVC by following these instructions and utilizing the code snippets that are provided.