我在我的 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
我想知道的另一件事是是否有可能让模型预先填充数据。