简体   繁体   中英

Set Query results from WCF service to MVC3 controller

I have a situation here. I am working with MVC3 web app. I wrote a WCF web service which communicates with DB. In a view I am trying to display all records from Student table.

Here is the code of StudentContorller where I make call to web service to get all student records:

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

Here is the definition of function in 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
    }

Here is student view 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>

So I am confused that in what dataType I should receive student records from service plus how would I return those records in form of list so that student view could process them. I dont want to change view code. Please help.

One way of doing that could be to create a Student class in your service. Say ServiceStudent .

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

then from your method : GetAllStudents() , you can fill up 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();
   }

then on client side:

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

I hope it will help.

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