我需要将数据提供给我的视图

时间:2012-11-21 20:05:27

标签: asp.net-mvc view model viewbag

我在我的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

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

1 个答案:

答案 0 :(得分:1)

请勿使用VIEWBAG获取模型数据。创建一个具有该点的viewmodel属性。

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

然后在你的控制器中:

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

return View(viewmodel);

修改 您似乎已经拥有了一个视图模型:@model TestingTool.ViewModels.ProcessModel 您可以为区域的视图模型添加属性吗?