2010-08-18 71 views
1

Server Error in '/' Application.Asp.net MVC 2應用程序錯誤

The model item passed into the dictionary is of type 'Develosoft4.Models.Cita', but this dictionary requires a model item of type 'Develosoft4.Models.CitaFormViewModel'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'Develosoft4.Models.Cita', but this dictionary requires a model item of type 'Develosoft4.Models.CitaFormViewModel'.

源錯誤:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

這是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> 

這是CitaFormViewModel.cs

using System.Web.Mvc; 

namespace Develosoft4.Models 
{ 
    public class CitaFormViewModel 
    { 
     private static CubiculoRepository cubiculosRepository = new CubiculoRepository(); 
     private static MateriaRepository materiasRepository = new MateriaRepository(); 
     private static ProfesorRepository profesorRepository = new ProfesorRepository(); 

    // Properties 
     public Cita Cita { get; private set; } 
     public SelectList Cubiculos { get; private set; } 
     public SelectList Materias { get; private set; } 
     public SelectList Profesores { get; private set; } 
    // Constructor 
     public CitaFormViewModel(Cita cita) 
     { 
      Cita = cita; 
      Cubiculos = new SelectList(cubiculosRepository.FindAllCubiculos(),"id","nombre", cita.cubiculo); 
      Materias = new SelectList(materiasRepository.FindAllMaterias(), "id", "nombre", cita.materia); 
      Profesores = new SelectList(profesorRepository.FindAllProfesores(), "id", "nombre", cita.profesor); 
     } 
    } 
} 

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"); 
     } 
    } 
} 
+0

任何幫助將非常感激。 謝謝大家的進步。 – Daniel 2010-08-18 05:11:05

+1

編輯您的問題並向我們顯示出現此錯誤的代碼。 – 2010-08-18 05:24:22

+0

歡迎來到Stack Overflow。如果下面的答案之一幫助回答你的問題,你應該將其標記爲接受的答案。 – Kelsey 2010-08-19 04:53:01

回答

0

你試圖通過模型Develosoft4.Models.Cita類型的對象時,它期待Develosoft4.Models.CitaFormViewModel類型的對象。

你可能有一個強類型的視圖,所以你需要傳遞它所期望的類型。

檢查控制器時,你應該在結尾有這樣的事情:

return View(new Develosoft4.Models.CitaFormViewModel() 
    { 
     // initializers 
    }); 

不知道你代碼實際上看起來像,所以這是刺在黑暗中:)

編輯:根據您添加的代碼,看起來您的Post版本的Create正在向視圖返回錯誤的類型。

你這樣做:

return View(cita); 

當你Create視圖期待一個CitaFormViewModel,所以你可能應該做的:

return View(new CitaFormViewModel(cita)); 

只是艾克您在Get版本Create觀點做。

0

The model item passed into the dictionary is of type 'Develosoft4.Models.Cita', but this dictionary requires a model item of type 'Develosoft4.Models.CitaFormViewModel'

看起來你正在返回從操作方法錯了型號。

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