While interacting with the database you need to bind the values into the views.
For doing that you need to create the model first which contain the properties which we want to bind.
So first we will create a model in the project.
public enum getstatus
{
success, failure
}
public class EmployeeModel
{
public int EmpId { get; set; }
[Required(ErrorMessage = "First Name is required")]
public string FirstName { get; set; }
public string MiddleName { get; set; }
[Required(ErrorMessage = "Last Name is required")]
public string LastName { get; set; }
[Required(ErrorMessage = "PhoneNo is required")]
public string PhoneNo { get; set; }
public string Address { get; set; }
public getstatus Status { get; set; }
public string msg { get; set; }
}
After this,we will create the database query for fetching data from the database.
public List<EmployeeModel> GetDetails()
{
List<EmployeeModel> EmployeeLst = new List<EmployeeModel>();
try
{
SqlDataReader reader = null;
using (SqlConnection sqlConnection = new SqlConnection(strConnString))
{
if (sqlConnection.State == ConnectionState.Closed)
{
sqlConnection.Open();
}
reader = SqlHelper.ExecuteReader(sqlConnection, CommandType.StoredProcedure, "uspGetUsers");
if (reader.HasRows)
{
while (reader.Read())
{
EmployeeLst.Add(new EmployeeModel
{
EmpId = reader["EmpId"] != null ? !string.IsNullOrEmpty(Convert.ToString(reader["EmpId"])) ? Convert.ToInt32(Convert.ToString(reader["EmpId"]).Trim()) : 0 : 0,
FirstName = reader["EmpFirstName"] != null ? Convert.ToString(reader["EmpFirstName"]) : String.Empty,
MiddleName = reader["EmpMiddleName"] != null ? Convert.ToString(reader["EmpMiddleName"]) : String.Empty,
LastName = reader["EmpLastName"] != null ? Convert.ToString(reader["EmpLastName"]) : String.Empty,
PhoneNo = reader["EmpPhoneNo"] != null ? Convert.ToString(reader["EmpPhoneNo"]) : String.Empty,
Address = reader["EmpAddress"] != null ? Convert.ToString(reader["EmpAddress"]) : String.Empty,
Status = getstatus.success
});
}
}
}
}
catch (Exception ex)
{
EmployeeLst.Add(new EmployeeModel
{
msg = ex.Message,
Status = getstatus.failure
});
}
return EmployeeLst;
}
Then at last we will bind this data from the view which we will create for the corresponding action.
<div class="form-horizontal">
<h4>EmployeeModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => Model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => Model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => Model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => Model.MiddleName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => Model.MiddleName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => Model.MiddleName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => Model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => Model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => Model.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => Model.PhoneNo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => Model.PhoneNo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => Model.PhoneNo, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => Model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => Model.Address, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => Model.Address, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
0 Comment(s)