Asp.net mvc 我需要将数据放到我的视图中

Asp.net mvc 我需要将数据放到我的视图中,asp.net-mvc,view,model,viewbag,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">

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

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

不要将VIEWBAG用于模型数据。创建具有点的viewmodel属性

class MyViewModel {
  public Collection<RegionModel> regions {get;set;}
  ...snip...
}
编辑: 看起来您已经有了一个视图模型:
@model TestingTool.ViewModels.ProcessModel

你能为区域的视图模型添加一个属性吗?

有趣的是,我以前尝试过,它工作得很好,我在视图中获取数据,但当我单击“创建”时,什么都没有发生,我的意思是什么都没有。出于某种奇怪的原因,我不明白,它现在正在工作。我使用Json将数据发送到控制器。就这样开始工作了。还是一样。
An expression tree may not contain a dynamic operation - mvc
class MyViewModel {
  public Collection<RegionModel> regions {get;set;}
  ...snip...
}
var viewmodel = new MyViewModel();
viewmodel.regions = region;

return View(viewmodel);