0

I receive a List of objects in my controller from my asp.net web application. How can I pass this list to the view2 and display it?

Controller

public class HomeController:Controller
{
 public ActionResult Index()
 {
  WCF.CommunicationServiceClient Client = new WCF.CommunicationClient("BasicHttpBinding_ICommunicationService");
  List<object> objectlist = Client.GetList("_something_");

  return View("ShowView");
 }
}

My biggest Problem is that this "object" is defined in another solution.

1
  • 5
    I recommend that you take a tutorial on ASP.NET MVC Commented Jan 20, 2016 at 12:49

4 Answers 4

0

My biggest Problem is that this "object" is defined in another solution.

You should define your object in another project. For example, 'DataTransferObjects'. Then you add a reference to this project in both your WCF Server project and your MVC (WCF Client) project. This way the class will be known by both.

In your DTO project:

[DataContract]
public class MyClass
{
    [DataMember]
    public int MyProperty { get; set; }
}

Then your code would be:

public class HomeController:Controller
{
 public ActionResult Index()
 {
  WCF.CommunicationServiceClient Client = new WCF.CommunicationClient("BasicHttpBinding_ICommunicationService");
  List<MyClass> objectlist = Client.GetList("_something_");

  return View("ShowView", objectlist);
 }
}

And your ShowView:

@model List<MyClass>

@for (i = 0; i < Model.Count; i++)
{
    @Html.EditorFor(m => Model[i].MyProperty)
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to pass the objectlist as view model to the view. I think the dynamic class will come in handy when defining your @model on the view.

Basic MVC really.

Comments

0

Create a strongly typed view then pass object to it like return View(objectlist );

Comments

0

You can use the dynamic instead of Object in the List like this, As you don't know what the object have grab.

 public class HomeController:Controller
 {
   public ActionResult Index()
    {
     WCF.CommunicationServiceClient Client = new WCF.CommunicationClient("BasicHttpBinding_ICommunicationService");
     List<dynamic> objectlist = Client.GetList("_something_");
     return View("ShowView");
    }
 }

Know more about dynamic click here Also one other Question that may be relates List with Dynamic Object Type

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.