0
我試圖從模型和從我的視圖到我的控制器的字符串傳遞一個列表。我從Home控制器到Dashboard.cshtml的數據成功,但是當我嘗試從模型傳遞一個列表到DetailController時,列表不會經過,但字符串TicketGroupName會。任何想法我做錯了什麼?MVC將多個參數傳遞給控制器,包括模型列表
Dashboard.cshtml
@model testlogin.Models.DashboardViewModel
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>@Model.AreaName</h2>
<table id="dashboardContainer" class="table">
<tr class="data-row-1">
<td class="tile text-center">
<a href="http://company.html"><img style="height: 125px" id="Image1" src="https://logo.jpg" /></a>
</td>
<td class="tile btn text-center">
<a href="@Url.Action("ShowTickets", "Detail", new { TicketList = Model.OpenNow, TicketGroupName = "Open Now" })">
<div>
<div class="row tile-head">
<h4>Open Now</h4>
</div>
<div class="row">
<h1><strong>@Model.OpenNow.Count</strong></h1>
</div>
</div>
</a>
</td>
</tr>
</table>
HomeController.cs
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Dashboard(string AreaName)
{
full_area_name = "Test Area";
var dash = GetDashData(board_number); //Not including it here, but this method works
dash.AreaName = full_area_name;
return View(viewName: "Dashboard", model: dash);
}
}
DashboardViewModel.cs
public class DashboardViewModel
{
public List<TicketModel> OpenNow { get; set; }
}
DetailController.cs
public class DetailController : Controller
{
public ActionResult ShowTickets(List<TicketModel> TicketList, string TicketGroupName)
{
TicketViewModel vm = new TicketViewModel()
{
GroupName = TicketGroupName,
Tickets = TicketList
};
return View(vm);
}
}
個
TicketViewModel.cs
public class TicketViewModel
{
public string GroupName { get; set; }
public IEnumerable<TicketModel> Tickets { get; set; }
}
ShowTickets.cshtml
@model testlogin.Models.TicketViewModel
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="container">
<div>
<h1>@Model.GroupName</h1>
</div>
<table class="table table-striped table-bordered table-hover">
<tr>
<th>@Html.LabelFor(model => model.Tickets.FirstOrDefault().TicketNbr)</th>
<th>@Html.LabelFor(model => model.Tickets.FirstOrDefault().Company_Name)</th>
<th>@Html.LabelFor(model => model.Tickets.FirstOrDefault().Priority_Description)</th>
<th>@Html.LabelFor(model => model.Tickets.FirstOrDefault().Summary)</th>
<th>@Html.LabelFor(model => model.Tickets.FirstOrDefault().Last_Update)</th>
<th>@Html.LabelFor(model => model.Tickets.FirstOrDefault().OwnerOrCloser)</th>
<th>@Html.LabelFor(model => model.Tickets.FirstOrDefault().Date_Entered_UTC)</th>
<th>@Html.LabelFor(model => model.Tickets.FirstOrDefault().Date_Responded_UTC)</th>
<th>@Html.LabelFor(model => model.Tickets.FirstOrDefault().Date_Closed_UTC)</th>
</tr>
@Html.DisplayFor(x => x.Tickets)
</table>
</div>
您不應該通過GET操作單擊鏈接傳遞列表。你應該再次在ShowTickets行爲方法中得到它。 – Shyju
您不能使用'Url.Action()'將一個集合傳遞給GET方法(查看您生成的html)。您將需要傳遞集合中每個項目的每個屬性作爲查詢字符串參數,這將是瘋狂的,並且可能超過查詢字符串限制)。在方法中再次獲取集合。 –
我明白了。我會嘗試併發布結果。謝謝你的幫助,你們倆! –