1

我这里有个情况。我正在使用 MVC3 网络应用程序。我编写了一个与 DB 通信的 WCF Web 服务。在一个视图中,我试图显示学生表中的所有记录。

这是StudentContorller我调用网络服务以获取所有学生记录的代码:

ServiceStudentClient client = new ServiceStudentClient();
client.GetAllStudents(); //What should be the return type??
return View(students.ToList()); //something like this??

下面是函数的定义StudentService

public void GetAllStudents()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["StudentCon"].ConnectionString);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "select * from Student";
        cmd.Connection = con;
        con.Open();
        SqlDataReader studentReader = cmd.ExecuteReader();
        con.Close();

        //Need to write code here to return students
    }

这是学生视图students.chtml

@model IEnumerable<StudentRegistrationPortal.Models.StudentModel>

@{
    ViewBag.Title = "All Students";
}

<h2>Index</h2>

<p>
   @Html.ActionLink("Add New Student", "Create")
</p>
<table>
<tr>
    <th>
        RollNumber
    </th>
    <th>
        Password
    </th>
    <th>
        Name
    </th>
    <th>
        Email
    </th>
    <th>
        Balance
    </th>
    <th></th>
</tr>

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.RollNumber)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Password)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Name)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Email)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Balance)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.SId }) |
        @Html.ActionLink("Details", "Details", new { id=item.SId }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.SId })
    </td>
</tr>
}

</table>

所以我很困惑,我应该以什么数据类型从服务接收学生记录,以及如何以列表的形式返回这些记录,以便学生视图可以处理它们。我不想更改视图代码。请帮忙。

4

1 回答 1

1

一种方法是在您的服务中创建一个学生类。说ServiceStudent

public class ServiceStudent
{
     //your student properties...
}

然后从你的方法:GetAllStudents(),你可以填写列表。

public List<ServiceStudent> GetAllStudents()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["StudentCon"].ConnectionString);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "select * from Student";
        cmd.Connection = con;
        con.Open();
        SqlDataReader studentReader = cmd.ExecuteReader();

        //Fill List of ServiceStudent from reader...

        con.Close();
   }

然后在客户端:

ServiceStudentClient client = new ServiceStudentClient();
List<Service.ServiceStudent> serviceList = client.GetAllStudents();    

//Now you need to map your ServiceStudent to ModelStudent here

List<ModelStudent> modelList = new List<ModelStudent>();

foreach(var serviceStudent in serviceList)
{
     ModelStudent model = new ModelStudent();
     model.property = serviceStudent.property;
     //Etc etc
     modelList.Add(model);
}
//Note : This is just rough code, For mapping you should use Mapper or write your custom method for mapping...

return View(modelList ); //pass Model Student here...

我希望它会有所帮助。

于 2013-07-28T07:57:20.683 回答