Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 如何在MVC3中使用两个IEnumerable_Asp.net Mvc 3 - Fatal编程技术网

Asp.net mvc 3 如何在MVC3中使用两个IEnumerable

Asp.net mvc 3 如何在MVC3中使用两个IEnumerable,asp.net-mvc-3,Asp.net Mvc 3,大家好,如何传递两个或多个IEnumerables列表以查看?这是我的动作控制器: ServicesClient client = new ServicesClient(); client.ClientCredentials.UserName.UserName = "service_test"; client.ClientCredentials.UserName.Password = ".."; client.ClientCredentials.ServiceCer

大家好,如何传递两个或多个IEnumerables列表以查看?这是我的动作控制器:

 ServicesClient client = new ServicesClient();
 client.ClientCredentials.UserName.UserName = "service_test";
 client.ClientCredentials.UserName.Password = "..";
            client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;

            ListCity[] city = client.GetCity();
            ListStreet[] street = client.GetStreet(2);

            return View(city);
var viewmodel = new MyViewModel{
                  City = client.GetCity(),
                  Street = client.GetStreet(2)
                };
return View(viewModel);
我的看法是:

model IEnumerable<ListCity>

@using icerik.EmlakServices 
@{

    Layout = "~/Views/Shared/Test.cshtml";
}


<select>

@foreach(var item in Model)
{

    <option>@Html.DisplayFor(x => item.CityName)</option>
}
</select>
模型IEnumerable
@使用icerik.emlak服务
@{
Layout=“~/Views/Shared/Test.cshtml”;
}
@foreach(模型中的var项目)
{
@DisplayFor(x=>item.CityName)
}

在这种情况下,您必须创建ViewModel。比如说:

public class MyViewModel{
  public ListCity[] City {get;set;}
  public ListStreet[] Street {get;set;}
}
然后在控制器中:

 ServicesClient client = new ServicesClient();
 client.ClientCredentials.UserName.UserName = "service_test";
 client.ClientCredentials.UserName.Password = "..";
            client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;

            ListCity[] city = client.GetCity();
            ListStreet[] street = client.GetStreet(2);

            return View(city);
var viewmodel = new MyViewModel{
                  City = client.GetCity(),
                  Street = client.GetStreet(2)
                };
return View(viewModel);
那么在你看来,

model MyViewModel
然后您可以在视图中获取集合,如
Model.City
,或
Model.Street

对于除基本情况外的所有情况,通常都会为几乎所有视图创建ViewModel。因此,正确地命名他们,这样你就知道他们在做什么。在这个例子中,我称它为
MyViewModel
,因为我不知道上下文

另外,我会将变量命名为
Cities
Streets
,因为它们是集合,而不仅仅是单个元素