将viewmodel与create视图一起使用

时间:2014-08-15 14:32:23

标签: asp.net-mvc view model

我有一个ViewModel,里面有两个模型,可以在显示数据时查找。我的问题是我想在Create.cshtml文件中添加foreach()。有什么想法吗?

---- Create.cshml -----

@model demo.Models.ViewModel

@{ ViewBag.Title = "Create Reference";}<h2>Create</h2>
@using (Html.BeginForm()) {    
@Html.AntiForgeryToken()    
@Html.ValidationSummary(true)    
<fieldset>
        <legend>Submission Form </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.Title)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Title)
        @Html.ValidationMessageFor(model => model.Title)
    </div> 

                 <div class="editor-field">
                     <!-- iterate through ExternalContentModel and make checkboxes. -->
                    @foreach (var item in Model.ExternalContentModel)
                    {
                        <label class="checkbox">
                             <input type="checkbox" name="users" value="@item.auth_lname"> @item.auth_lname
                        </label>
                    }
                </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.OrganizationName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.OrganizationName)
        @Html.ValidationMessageFor(model => model.OrganizationName)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Address)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Address)
        @Html.ValidationMessageFor(model => model.Address)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.City)
    </div>      

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>

}

------ -----控制器

    //
    // GET: /Create
    public ActionResult Create() <=== can this be the problem????
    { 
        demo.Models.ViewModel vm = new demo.Models.ViewModel();            
        vm.ExternalContentModel = _repository.ExternalContent();

        // Return the content from the External Model to the Create. 
        return View(vm); 
    }

    //
    // POST: /MailingReferences/Create
    [HttpPost]
    [ValidateAntiForgeryToken]

    public ActionResult Create(Reference reference) <=== can this be the problem????
    {
        if (ModelState.IsValid)
        {
            db.References.Add(reference);
            db.SaveChanges();
            return RedirectToAction("Index");
        }             
        return View(reference);
    }

1 个答案:

答案 0 :(得分:0)

如果您有不同的型号,为什么不为每个型号创建一个编辑器模板?

您可以通过创建一个名为&#34; EditorTemplates&#34;的文件夹来完成此操作。在您的Create.cshtml视图所在的同一文件夹中。现在将模型的视图添加到该文件夹​​中。视图的名称应与模型类相同。例如。一个名为FooBarModel的类将有一个名为FooBarModel.cshtml的编辑器模板。

然后你可以通过@Html.EditorFor(x =&gt; x.FooBar)来使用编辑器模板