简体   繁体   English

在asp.net MVC视图中显示列表

[英]Display list in asp.net MVC view

I receive a List of objects in my controller from my asp.net web application. 我从asp.net Web应用程序收到控制器中的对象列表。 How can I pass this list to the view2 and display it? 如何将此列表传递给view2并显示它?

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. 我最大的问题是,此“对象”是在另一个解决方案中定义的。

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. 我认为在视图上定义@model时,动态类会派上用场。

Basic MVC really. 真正的基本MVC。

创建一个强类型的视图,然后像return View(objectlist );一样将对象传递给它return View(objectlist );

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 在此处了解更多有关动态的信息 单击此处可能还有一个其他问题与动态对象类型的列表有关

My biggest Problem is that this "object" is defined in another solution. 我最大的问题是,此“对象”是在另一个解决方案中定义的。

You should define your object in another project. 您应该在另一个项目中定义对象。 For example, 'DataTransferObjects'. 例如,“ DataTransferObjects”。 Then you add a reference to this project in both your WCF Server project and your MVC (WCF Client) project. 然后,在WCF Server项目和MVC(WCF客户端)项目中都添加对此项目的引用。 This way the class will be known by both. 这样,该类将被双方都知道。

In your DTO project: 在您的DTO项目中:

[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: 和你的ShowView:

@model List<MyClass>

@for (i = 0; i < Model.Count; i++)
{
    @Html.EditorFor(m => Model[i].MyProperty)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM