2013-07-28 59 views
1

我有一個情況在這裏。我正在使用MVC3網絡應用程序。我寫了一個與DB通信的WCF Web服務。在視圖中,我試圖顯示學生表中的所有記錄。設置查詢從WCF服務結果MVC3控制器

這裏是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> 

所以,我很困惑,在什麼樣的dataType我應該從服務接收學生檔案加上我怎麼會以列表形式返回那些記錄,以便學生可以查看處理它們。我不想更改視圖代碼。請幫忙。這樣做的

回答

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

我希望這將有助於。

+0

完美。非常感謝:) – Azeem

+0

不客氣... –