MVC将多个参数传递给控制器​​,包括模型中的列表

时间:2017-09-15 23:28:48

标签: c# asp.net-mvc razor

我正在尝试将模型中的列表和我视图中的字符串传递给控制器​​。我成功地从Home控制器获取数据到Dashboard.cshtml,但是当我尝试将模型从模型传递给DetailController时,列表不会通过,而是字符串TicketGroupName。对我做错了什么的想法?

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>

1 个答案:

答案 0 :(得分:0)

正如@Shyju和@Stephen_Muecke所建议的那样,我重新安排了控制器中的代码,以便在视图调用动作时从Http缓存中再次获取数据。

为了清楚以防其他人正在搜索此答案,您无法将视图中的列表(或IEnumerable)对象传递给控制器​​。必须在控制器中再次收集列表数据。

谢谢你的帮助,伙计们!