Dam*_*lds 4 asp.net-mvc razor asp.net-mvc-4
我是mvc4的新用户,所以请放心,我试图将多个目的地名称从控制器传递到我的视图.如何在不创建更多动作结果和对象的情况下执行此操作.我想使用字符串数组创建一个目标名称的表.这可能是任何帮助将不胜感激.先感谢您.
Controller:
public ActionResult Destinations()
{
string[] arrivalAirport = new string[4] { "london", "paris", "berlin",
"manchester" };
Destination dest = new Destination();
dest.arrivalName = arrivalAirport[2];
return View(dest);
}
Model:
public class Destination
{
public string arrivalName { get; set; }
public string arrivalCode { get; set; }
}
View:
@model Flight.Models.Destination
@{
ViewBag.Title = "Destinations";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<table>
<tr>
<td>@Html.Label("Arrival Name")</td>
<td>@Model.arrivalName</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
您的问题的措辞非常混乱,但听起来您想要将多个Destination对象传递给您的视图.如果是这种情况,只需将多个目标对象传递给视图即可.
控制器:
public ActionResult Destinations()
{
string[] arrivalAirport = new string[4] { "london", "paris", "berlin",
"manchester" };
var airports = new List<Destination>();
foreach( var airport in arrivalAirport )
{
airports.Add( new Destination() { arrivalName = airport } );
}
return View(airports);
}
Run Code Online (Sandbox Code Playgroud)
视图:
@model List<Flight.Models.Destination>
<table>
@foreach( var dest in Model )
{
<tr>
<td>Arrival Name</td>
<td>@dest.arrivalName</td>
</tr>
}
</table>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18470 次 |
| 最近记录: |