简体   繁体   中英

ASP.NET MVC 4 data access

I am trying to show the employee details in a table. I created a model class EmployeeDetail , an EmployeeController and a view in EmployeeDetails.cshtml , plus a class for accessing the database.

EmployeeDetails is my model class:

[Serializable]
public class EmployeeDetails
{
    private int _eid;
    private string _ename;
    private string _eaddress;
    private string _gender;
    private string _emobileno;
    private bool _status;
    private DateTime _ejoiningdate;
    private DateTime _eleavedate;

    #region ================= Code start for public variable =========================

    [DataObjectField(true,true,false)]
    public int eid {
        get { return _eid; }
        set { _eid = value; }
    }

    [DataObjectField(true, true, false)]
    public string ename
    {
        get { return _ename; }
        set { _ename = value; }
    }

    [DataObjectField(true, true, false)]
    public string eaddress
    {
        get { return _eaddress; }
        set { _eaddress = value; }
    }

    [DataObjectField(true, true, false)]
    public string gender
    {
        get { return _gender; }
        set { _gender = value; }
    }

    [DataObjectField(true, true, false)]
    public string emobileno
    {
        get { return _emobileno; }
        set { _emobileno = value; }
    }

    [DataObjectField(true, true, false)]
    public bool status
    {
        get { return _status; }
        set { _status = value; }
    }

    [DataObjectField(true, true, false)]
    public DateTime ejoiningdate
    {
        get { return _ejoiningdate; }
        set { _ejoiningdate = value; }
    }

    [DataObjectField(true, true, false)]
    public DateTime eleavedate
    {
        get { return _eleavedate; }
        set { _eleavedate = value; }
    }

    #endregion ==================== code end for public variable =================
}

Here is my EmployeeController :

public class EmployeeController : Controller
{
    // GET: /Employee/
    private Employeecon db = new Employeecon();

    public ActionResult EmployeeDetails()
    {
        var studentList = new List<EmployeeDetails>{ 
                        new EmployeeDetails() { eid = 1, ename = "John", eaddress = "teszxs" } ,
                        new EmployeeDetails() { eid = 2, ename = "Steve",  eaddress = "jfdsk" } ,
                        new EmployeeDetails() { eid = 3, ename = "Bill",  eaddress =  "jfdsk" } ,
                        new EmployeeDetails() { eid = 4, ename = "Ram" , eaddress =  "jfdsk" } ,
                        new EmployeeDetails() { eid = 5, ename = "Ron" , eaddress =  "jfdsk" } ,
                        new EmployeeDetails() { eid = 4, ename = "Chris" , eaddress =  "jfdsk" } ,
                        new EmployeeDetails() { eid = 4, ename = "Rob" , eaddress =  "jfdsk" } 
                    };
        return View();
    }
}

I created EmployeeDetails.cshtml as a Razor view:

@model IEnumerable<Employee.Models.EmployeeDetails>

<html>
<head>
    <title>Employee Details</title>
</head>
<body>
    <div align="center">
      <b> Employee Details</b> 

    </div>
    <div align="right">

<p>
@Html.ActionLink("New Employee", "NewEmployee")
</p>
    </div>
<table align="center" cellpadding="5" cellspacing="5" style="border:1px thin 
black;" frame="box">
<tr>
    <th>
        Name
    </th>
    <th>
      Address

    </th>
    <th>
        Mobile no

    </th>
    <th>
        Joining Date
    </th>
</tr>
@foreach(var r in Model)
{
<tr>
    <td>
       @* @r.ename;*@

            @Html.DisplayNameFor(m => r.ename); //but when I am declaring like that its showing error the call is ambiguous between the following methods or properties 
    </td>
</tr>

<tr>
    <td>
        @*@r.eaddress;*@
            @Html.DisplayNameFor(m => r.eaddress);
    </td>
</tr>
<tr>
    <td>
      @*  @r.emobileno;*@
            @Html.DisplayNameFor(m => r.emobileno);
    </td>
</tr>
<tr>
    <td>
      @*  @r.ejoiningdate;*@
            @Html.DisplayNameFor(m => r.ejoiningdate);
    </td>
</tr>
}
</table>
    </body>
</html>

I am creating a separate folder for data access. I created a data context file Employeecon.cs :

public class Employeecon : DbContext
{
    public Employeecon() : base("EmployeeContext")
    { 
    }

    public DbSet<EmployeeDetails> emp { get; set; }
}

And I added a connection string to the web.config file:

 <add name="EmployeeContext" 
      connectionString="Data Source=LEVIOZA;Initial Catalog=Manali;Persist Security Info=True;User ID=sa;Password=pedcall" 
      providerName="System.Data.SqlClient" />

I want to connect to a SQL Server database, instead of local db, or I don't want to create database in the App_Data folder. Like we are did in the asp.net using SQL Server we pass the connection string using SqlConnection like that.

I am trying to connect but I don't know if it's working or not .

I don't know whats wrong with this program. I am trying to learn ASP.NET MVC and this is my first try. Please help me.

So you actually did a good job so far, let me point you to the rest.

Firstly , with database connection a side, you did created a simulation of data, which i assume you wonder why they did not shown in the view.

This is because you did not asked the controlled to use your data as model for the view. Yes, you did specify which type the model is, but you didn't supply it with data.

To fix it, pass the View() method the model instance you want, so it can bind it together.

return View(studentList);

Secondly , you want to extract those information from database, you only lacking querying the information you want from database and return it as model.

For example, lets create another url called /EmployeeDetailsFromDb

public ActionResult EmployeeDetailsFromDb()
{
    var studentList = db.EmployeeDetails; // .EmployeeDetails should be changed to the table\dataset you want.
    return View(studentList);
}

This will work, assuming the EmployeeDetails(if exists) is actually a collection of EmployeeDetails entities, which is what the view .cshtml expects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM