Asp.net mvc 2 Asp.net MVC 2应用程序错误

Asp.net mvc 2 Asp.net MVC 2应用程序错误,asp.net-mvc-2,Asp.net Mvc 2,“/”应用程序中出现服务器错误 传递到字典中的模型项的类型为“developsoft4.Models.Cita”,但此字典需要类型为“developsoft4.Models.CitaFormViewModel”的模型项 描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源 异常详细信息:System.InvalidOperationException:传入字典的模型项的类型为“developsoft4.Models.Cita”,但此字典需要类

“/”应用程序中出现服务器错误

传递到字典中的模型项的类型为“developsoft4.Models.Cita”,但此字典需要类型为“developsoft4.Models.CitaFormViewModel”的模型项

描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源

异常详细信息:System.InvalidOperationException:传入字典的模型项的类型为“developsoft4.Models.Cita”,但此字典需要类型为“developsoft4.Models.CitaFormViewModel”的模型项

源错误:

在执行当前web请求期间生成了未经处理的异常。有关异常的起源和位置的信息可以使用下面的异常堆栈跟踪来识别

这是引发错误的Create.aspx:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Develosoft4.Models.CitaFormViewModel>" %>
    <h2>Create</h2>

    <% using (Html.BeginForm()) {%>
        <%: Html.ValidationSummary(true) %>

        <fieldset>
            <legend></legend>
            
            
            <div class="editor-label">
                <%: Html.LabelFor(model => model.Cita.materia)%>
            </div>
            <div class="editor-field">
            
                <%: Html.DropDownListFor(model => model.Cita.materia, Model.Materias)%>
                <%: Html.ValidationMessageFor(model => model.Cita.materia)%>
            </div>
            
            <div class="editor-label">
                <%: Html.LabelFor(model => model.Cita.cubiculo)%>
            </div>
            <div class="editor-field">
                <%: Html.DropDownListFor(model => model.Cita.cubiculo, Model.Cubiculos)%>
                <%: Html.ValidationMessageFor(model => model.Cita.cubiculo)%>
            </div>
            
            <div class="editor-label">
                <%: Html.LabelFor(model => model.Cita.profesor)%>
            </div>
            <div class="editor-field">
                <%: Html.DropDownListFor(model => model.Cita.profesor, Model.Profesores)%>
                <%: Html.ValidationMessageFor(model => model.Cita.profesor)%>
            </div>
            
            <div class="editor-label">
                <%: Html.LabelFor(model => model.Cita.fecha)%>
            </div>
            <div class="editor-field">
                
                <%: Html.ValidationMessageFor(model => model.Cita.fecha)%>
                    <form>
   <input type="text" name="fecha" id="campofecha">
</form>
            </div>
            
            <div class="editor-label">
                <%: Html.LabelFor(model => model.Cita.horaInicio)%>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Cita.horaInicio)%>
                <%: Html.ValidationMessageFor(model => model.Cita.horaInicio)%>
            </div>
            
            <div class="editor-label">
                <%: Html.LabelFor(model => model.Cita.horaFinal)%>
            </div>
            <div class="editor-field">
                <%: Html.TextBoxFor(model => model.Cita.horaFinal)%>
                <%: Html.ValidationMessageFor(model => model.Cita.horaFinal)%>
            </div>
            
            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>

    <% } %>

    <div>
        <%: Html.ActionLink("Back to List", "Index") %>
    </div>
CitaController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Develosoft4.Models;


namespace Develosoft4.Controllers
{
    public class CitaController : Controller
    {
        CitaRepository repository = new CitaRepository();

        //
        // GET: /Cita/
       [Authorize (Roles= "director")]
       public ActionResult Index(int page = 0)
        {
            const int pageSize = 10;

            var citas = repository.FindAllCitas();
            var paginatedCita = new PaginatedList<Cita>(citas,page,pageSize);
            return View(paginatedCita);
        }
        //
        // GET: /Cita/Details/2

        public ActionResult Details(int id)
        {
            Cita cita = repository.GetCita(id);

            if (cita == null)
                return View("NotFound");
            else
                return View("Details", cita);
        }
        //
        // GET: /Cita/Edit/2

        public ActionResult Edit(int id)
        {
            Cita cita = repository.GetCita(id);
            CitaFormViewModel viewModel = new CitaFormViewModel(cita);
            return View(viewModel);
        }

        //
        // POST: /Cita/Edit/2
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection formValues)
        {
            Cita cita = repository.GetCita(id);

            try
            {
                UpdateModel(cita);
                repository.Save();
                return RedirectToAction("Details", new { id = cita.id });
            }
            catch
            {
                //ModelState.AddRuleViolations(materia.GetRuleViolations());

                return View(cita);
            }
        }

        //
        // GET: /Cita/Create
        public ActionResult Create()
        {
           Cita cita = new Cita();
            return View( new CitaFormViewModel( cita));
        }
        //
        // POST: /Cita/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Cita cita)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    repository.Add(cita);
                    repository.Save();
                   return RedirectToAction("Details", new { id = cita.id });
                }
                catch
                {
                    //ModelState.AddRuleViolations(materia.GetRuleViolations());
                }
            }

            return View(cita);
        }

        //
        // HTTP GET: /Cita/Delete/1

        public ActionResult Delete(int id)
        {
            Cita cita = repository.GetCita(id);

            if (cita == null)
                return View("NotFound");
            else
                return View();
        }

        // actitud
        // HTTP POST: /Cita/Delete/1

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id, string confirmButton)
        {
            Cita cita = repository.GetCita(id);

            if (cita == null)
                return View("NotFound");

            repository.Delete(cita);
            repository.Save();

            return View("Deleted");
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
使用软件4.模型;
名称空间4.控制器
{
公共类控制器:控制器
{
CitaRepository repository=新建CitaRepository();
//
//获取:/Cita/
[授权(角色=“董事”)]
公共操作结果索引(int page=0)
{
常量int pageSize=10;
var citas=repository.FindAllCitas();
var paginatedCita=新的分页列表(citas,页面,页面大小);
返回视图(paginatedCita);
}
//
//获取:/Cita/Details/2
公共行动结果详细信息(int id)
{
Cita-Cita=repository.GetCita(id);
如果(cita==null)
返回视图(“未找到”);
其他的
返回视图(“详细信息”,cita);
}
//
//获取:/Cita/Edit/2
公共操作结果编辑(int id)
{
Cita-Cita=repository.GetCita(id);
CitaFormViewModel viewModel=新的CitaFormViewModel(cita);
返回视图(viewModel);
}
//
//POST:/Cita/Edit/2
[接受动词(HttpVerbs.Post)]
公共操作结果编辑(int id、FormCollection formValues)
{
Cita-Cita=repository.GetCita(id);
尝试
{
更新模型(cita);
Save();
返回RedirectToAction(“Details”,new{id=cita.id});
}
接住
{
//ModelState.addRuleVillences(material.getRuleVillences());
返回视图(cita);
}
}
//
//获取:/Cita/Create
公共操作结果创建()
{
Cita Cita=新的Cita();
返回视图(新CitaFormViewModel(cita));
}
//
//POST:/Cita/创建
[接受动词(HttpVerbs.Post)]
公共行动结果创建(Cita Cita)
{
if(ModelState.IsValid)
{
尝试
{
添加(cita);
Save();
返回RedirectToAction(“Details”,new{id=cita.id});
}
接住
{
//ModelState.addRuleVillences(material.getRuleVillences());
}
}
返回视图(cita);
}
//
//HTTP GET:/Cita/Delete/1
公共操作结果删除(int id)
{
Cita-Cita=repository.GetCita(id);
如果(cita==null)
返回视图(“未找到”);
其他的
返回视图();
}
//活动
//HTTP POST:/Cita/Delete/1
[接受动词(HttpVerbs.Post)]
公共操作结果删除(int-id,字符串confirButton)
{
Cita-Cita=repository.GetCita(id);
如果(cita==null)
返回视图(“未找到”);
删除(建训局);
Save();
返回视图(“已删除”);
}
}
}
传递到字典中的模型项的类型为“developsoft4.Models.Cita”,但此字典需要类型为“developsoft4.Models.CitaFormViewModel”的模型项

看起来您从操作方法返回了错误的模型类型

//This is where I think the error is. It is expecting a CityFormViewModel instead of a Cita object
return View(citaModel);

您试图将类型为
developsoft4.Models.Cita
的对象传递给您的模型,但它需要类型为
developsoft4.Models.CitaFormViewModel
的对象

您可能有一个强类型视图,因此需要将其传递为所需的类型

当你应该在最后有这样的东西时,检查你的控制器:

return View(new Develosoft4.Models.CitaFormViewModel()
    {
        // initializers
    });
不知道您编写的代码实际上是什么样子,所以这是暗中刺伤:)

编辑:根据您添加的代码,您的
Post
版本的
Create
将错误的类型返回到视图中

您正在这样做:

return View(cita); 
当您的
Create
视图需要一个
CitaFormViewModel
时,您可能应该执行以下操作:

return View(new CitaFormViewModel(cita));

请在
Get
版本的
Create
视图中查看您所做的一切。

非常感谢您的帮助。感谢所有高级用户。请编辑您的问题并向我们展示产生此错误的代码。欢迎使用堆栈溢出。如果下面的答案中有一个有助于回答您的问题,您应该将其标记为已接受的答案。
return View(new CitaFormViewModel(cita));