0

我在我的 mvc 内网中有一个视图来创建进程。

在该视图中,我有一个需要填充数据的表。

这是我的看法

@model TestingTool.ViewModels.ProcessModel
@{
    ViewBag.Title = "Create";
}
<h2>
    Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Process</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Code)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Code)
            @Html.ValidationMessageFor(model => model.Code)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Description)
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.Description)
            @Html.ValidationMessageFor(model => model.Description)
        </div>
        <table>
            <thead>
                <tr>
                    <th>
                        Region
                    </th>
                    <th>
                        Owner
                    </th>
                </tr>
            </thead>
            <tbody>
               @if (Model != null)
               {
                   foreach (var item in ViewBag.Regions)
                   {
                       <tr>

                           <td>
                             @Html.DisplayTextFor(i => item.Name)
                           </td>

                           <td>
                               @Html.DropDownListFor(i => item.User, new SelectList(item.User, "Id", "Name"))
                           </td>

                       </tr>
                   }
               }
            </tbody>
        </table>
        <p>
            <input type="button" value="Create" />
        </p>
    </fieldset>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

这是我的控制器

//
        // GET: /Process/Create

        public ActionResult Create()
        {
            Collection<RegionModel> region = new Collection<RegionModel>();

            foreach (var regions in _db.Regions)
            {
                if (regions.Name != "Europe")
                {
                    RegionModel iRegion = new RegionModel
                        {
                            Name = regions.Name,
                            Id = regions.Id,
                            User = new Collection<UserModel>()
                        };

                    foreach (User user in _db.Users)
                    {
                        UserModel iUser = new UserModel { Id = user.Id, Name = user.Name };

                        iRegion.User.Add(iUser);
                    }

                    region.Add(iRegion);
                }
            }

            ViewBag.Regions = region;

            return View();
        }

我认为可以迭代 ViewBag,但出现此错误

An expression tree may not contain a dynamic operation - mvc

我想知道的另一件事是是否有可能让模型预先填充数据。

4

1 回答 1

1

不要将 VIEWBAG 用于模型数据。创建一个具有位置的视图模型属性。

class MyViewModel {
  public Collection<RegionModel> regions {get;set;}
  ...snip...
}

然后在你的控制器中:

var viewmodel = new MyViewModel();
viewmodel.regions = region;

return View(viewmodel);

编辑: 看起来你已经有一个视图模型:@model TestingTool.ViewModels.ProcessModel 你可以为区域的视图模型添加一个属性吗?

于 2012-11-21T20:09:13.827 回答