无法在视图内呈现局部视图

时间:2016-01-07 09:32:13

标签: asp.net-mvc entity-framework

我的控制器

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using C3CardKYC1.Models;

namespace C3CardKYC1.Controllers
{
    public class HomeControllerController : Controller
    {
        private ConnectionString db = new ConnectionString();

        //
        // GET: /HomeController/

        public ActionResult Index()
        {
            return View(db.DetailsEntries.ToList());
        }



        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /HomeController/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(DetailsEntry detailsentry)
        {
            if (ModelState.IsValid)
            {
                db.DetailsEntries.Add(detailsentry);
                db.SaveChanges();
                //return RedirectToAction("Index");
            }

            return View(detailsentry);
        }
       [HttpGet]
      public ActionResult displaydocument()
        {
            ViewBag.doctype = new SelectList(db.DocTypeMasters,"Id","DocTypeName");

            return PartialView();
           // return PartialView();
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}

这是我的创建视图代码。

@model C3CardKYC1.Models.DetailsEntry

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>DetailsEntry</legend>

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

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

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

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

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

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

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

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

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@Html.Partial("displaydocument",Model)

这是我的displaydocument视图代码

@model C3CardKYC1.Models.DocTypeMaster

Select Document Type: @Html.DropDownList("DocTypeName", "Select");

我想生成一个视图create.cshtl。在里面,我想渲染displaydocumentview。我收到了这个错误。信息:没有类型为'IEnumerable'的ViewData项具有键'DocTypeName'。

1 个答案:

答案 0 :(得分:0)

在离开控制器操作方法之前,您应该在DocTypeName中定义ViewBag

ViewBag.DocTypeName = List<SelectListItem>()
{new SelectListItem("item1", 1), new SelectListItem("item2", 2)};