簡體   English   中英

將WCF服務的查詢結果設置為MVC3控制器

[英]Set Query results from WCF service to MVC3 controller

我這里有個情況。 我正在使用MVC3網絡應用程序。 我編寫了一個與DB通信的WCF Web服務。 在一個視圖中,我試圖顯示學生表中的所有記錄。

這是StudentContorller的代碼,我在其中調用Web服務以獲取所有學生記錄:

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>

所以我很困惑,我應該從哪種dataType接收來自服務的學生記錄,以及我將如何以列表的形式返回這些記錄,以便學生視圖可以處理它們。 我不想更改視圖代碼。 請幫忙。

一種方法是在服務中創建一個Student類。 ServiceStudent

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

然后從您的方法: GetAllStudents() ,您可以填充List。

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...

希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM