将IEnumerable <somemodel>和SomeModel传递给View </somemodel>

时间:2014-08-11 22:50:20

标签: asp.net-mvc list ienumerable

我正在使用:

@model IEnumerable<SomeModel1>

在我看来。我不能改变它,因为我们的应用程序中的多个控制器返回一些类型为

的变量
List<SomeModel1>

我还需要为模态窗口传递相同的模型(SomeModel1)而不使用IEnumerable:

@model WebApplication8.Models.ManageUserViewModel

有没有办法在不将模型放入父模型的情况下实现这一目标?

更新

查看页面:

<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <table class="table table-bordered table-hover" id="tab_logic">
                <thead>
                    <tr>
                        <th class="text-center">
                            Active
                        </th>
                        <th class="text-center">
                            Email Address
                        </th>
                        <th class="text-center">
                            First name
                        </th>
                        <th class="text-center">
                            Last Name
                        </th>
                        <th class="text-center">
                            Company
                        </th>
                        <th class="text-center">
                            permissions
                        </th>
                        <th class="text-center">
                            Machine<p>Limit</p>
                        </th>
                        <th class="text-center">
                            Machines<p>Consumed</p>
                        </th>
                    </tr>
                </thead>
                <tbody>




                    @foreach (var item in Model)
                    {


                        <tr>
                            <td  style="display:none" >@Html.DisplayFor(model => item.UserId)</td>
                            <td align="center">
                                <div class="btn-group btn-toggle">
                                    @if (item.ActiveUser)
                                    {
                                        <button class="btn btn-sm btn-success active">ON</button>  
                                        <button class="btn btn-sm btn" onclick="DeActivateUser('@item.UserId')">OFF</button>  
                                    }
                                    else
                                    {
                                        <button class="btn btn-sm btn" onclick="ActivateUser('@item.UserId')">ON</button>
                                        <button class="btn btn-sm btn-success active">OFF</button>
                                    }
                                </div>


                            </td>
                            <td>@Html.DisplayFor(model => item.EmailAddress)</td>
                            <td>@Html.DisplayFor(model => item.FirstName)</td>
                            <td>@Html.DisplayFor(model => item.LastName)</td>
                            <td>@Html.DisplayFor(model => item.Company)</td>
                            <td>
                                @Html.DisplayFor(model => item.UserPermission)
                                <a href="#" class="dropdown-toggle glyphicon glyphicon-chevron-down" data-toggle="dropdown"><b class="dropdown-menu"></b> </a>
                                <ul class="dropdown-menu">
                                    <li><a onclick="ChangePermission('@item.UserId', '3')">View Only</a></li>
                                    <li><a onclick="ChangePermission('@item.UserId', '4')">View - Print</a></li>
                                    <li><a onclick="ChangePermission('@item.UserId', '5')">View - One Print</a></li>
                                    <li><a onclick="ChangePermission('@item.UserId', '6')">Expire by Use - 5 Opens</a></li>
                                    <li><a onclick="ChangePermission('@item.UserId', '7')">Expire After 5 Days</a></li>
                                </ul>
                            </td>
                            <td align="center">@Html.DisplayFor(model => item.MachineLimit)</td>
                            <td align="center">@Html.DisplayFor(model => item.MachineCount)</td>
                        </tr>
                   }

                </tbody>
            </table>
        </div>
    </div>
</div>




    <section id="addUser">

    @using (Html.BeginForm("Manage", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
    {
        @Html.AntiForgeryToken()

        <h2>Add Authorized User(s): </h2>
        <hr style="height:7pt;" />

          <!--  <hr style="height:0pt; visibility:hidden;" /> -->
<button class="btn btn-default btn-success btn-lg" data-target="#modalId" data-toggle="modal" type="button">
    <span class="glyphicon glyphicon-plus"></span> ADD NEW USER
</button>
        <div class="modal fade" id="modalId" tabindex="-1">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-title">Add Authorized User(s)</h4>
                    </div>
                    <div class="modal-body">
                        @Html.ValidationSummary()

                        <!--  <div class="col-md-4">   -->

                        <div class="form-group">

                            @Html.LabelFor(m => m. ().FirstName, new { @class = "col-md-2 control-label" })
                            <div class="col-md-10">
                                @Html.TextBoxFor(m => m.FirstOrDefault().FirstName, new { @class = "form-control" })
                            </div>
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(m => m.FirstOrDefault().LastName, new { @class = "col-md-2 control-label" })
                            <div class="col-md-10">
                                @Html.TextBoxFor(m => m.FirstOrDefault().LastName, new { @class = "form-control" })
                            </div>
                        </div>




                        <div class="form-group">
                            @Html.LabelFor(m => m.FirstOrDefault().EmailAddress, new { @class = "col-md-2 control-label" })
                            <div class="col-md-10">
                                @Html.TextBoxFor(m => m.FirstOrDefault().EmailAddress, new { @class = "form-control" })
                            </div>
                        </div>


                        <div class="form-group">
                            @Html.LabelFor(m => m.FirstOrDefault().Company, new { @class = "col-md-2 control-label" })
                            <div class="col-md-10">
                                @Html.TextBoxFor(m => m.FirstOrDefault().Company, new { @class = "form-control" })
                            </div>
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(m => m.FirstOrDefault().UserPermission, new { @class = "col-md-2  control-label" })
                            <div class="col-md-3  control-label">

                                @Html.DropDownListFor(m => m.FirstOrDefault().UserPermission, new SelectList(new List<Object>
               {
                   new { value = "2" , text = "View Only"},
                   new { value = "3" , text = "View - Print"  },
                   new { value = "4" , text = "View - One Print" },
                   new { value = "5" , text = "Expire by 5 Use" },
                   new { value = "6" , text = "Expire by 5 Date" }

               },
               "value", "text", "ViewOnly"))
                            </div>

                            @Html.LabelFor(m => m.FirstOrDefault().MachineLimit, new { @class = "col-md-2  control-label" })
                            <div class="col-md-2  control-label">

                                @Html.DropDownListFor(m => m.FirstOrDefault().MachineLimit, new SelectList(new List<Object>
               {
                   new { value = "1" , text = "1"},
                   new { value = "2" , text = "2"  },
                   new { value = "3" , text = "3" },
                   new { value = "4" , text = "4" },
                   new { value = "5" , text = "5" }

               },
               "value", "text", 1))
                            </div>

                        </div>




                    </div>
                    <div class="modal-footer">
                        <div class="col-md-3 col-md-10  control-label">
                            <input type="submit" class="btn btn-default" value="Select/Add" name="Action:Insert" />
                        </div>
                    </div>
                </div>
            </div>
        </div>

    }

</section>

3 个答案:

答案 0 :(得分:1)

不建议将多种类型传递到单个视图中,这可能意味着您以某种方式滥用视图。您有3个选择:

不同观点

更改您正在执行的操作并为每种类型制作视图。

动态类型

使用dynamic作为您的模型类型,但这意味着您的视图代码中充斥着if语句 - 这不是一个好的设计。

换入列表

SomeModel对象包装在List中,如下所示:

SomeModel myModel;
return View(new List<SomeModel> { myModel };

答案 1 :(得分:0)

如果您必须使用SomeModel1, 您可以通过上面的@Model.firstordefault().{class properties}声明来使用IEnumerable

答案 2 :(得分:0)

使用ViewBag

// controller
ViewBag.SomeModel = someModel;

// view
@ViewBag.SomeModel.SomeProperty