Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Bind value in view in MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 37
    Comment on it

    While getting values from the database we need to map the values into the views by writing the code for that.

     

     

    So for doing that we need to get the data first from the action controller then we will bind it with the view.

     

    public ActionResult Index()
            {
                EmpRep = new EmployeeRepository();
                List<EmployeeModel> EmpMod = new List<EmployeeModel>();
                EmpMod = EmpRep.GetDetails().ToList();
                return View(EmpMod);
            }

     

     

    This is the Index action which is used to get the details of the employee 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 we will bind these values coming from the database collecting into the entity and then binding at last into the views.

     

    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.EmpId)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.FirstName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.MiddleName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.LastName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.PhoneNo)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Address)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.msg)
            </th>
            <th></th>
        </tr>
    
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.EmpId)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.FirstName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.MiddleName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.LastName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.PhoneNo)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Address)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.msg)
                </td>
                <td>
                    <a class="lnkEdit" id='@item.EmpId'>Edit</a> | |
                    @Html.ActionLink("Details", "Details", new { id = item.EmpId }, new { @class = "lnkDetail" }) |
                    @Html.ActionLink("Delete", "Delete", new { id = item.EmpId }, new { @class = "lnkDelete" })
                </td>
            </tr>
        }
    
    </table>
    

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: