【问题标题】:Dictionary error while passing a class in to a cshtml file将类传递给cshtml文件时出现字典错误
【发布时间】:2014-04-10 10:52:13
【问题描述】:

我试图将 2 个类传递到一个 cshtml 文件中,但一直收到错误消息,我传递了一个“JavaTutorial.Models.Quiz”类型的模型,但是这个字典需要一个“JavaTutorial.Models”类型的模型项.评估'。”

我不明白,我在创建一个单独的类之后,现在只想传入一个类,但仍然出现错误。

我有一个类评估,我试图在 cshtml 中调用它,但一个测验类一直在干扰,但我不知道为什么..

cshtml:

    @model JavaTutorial.Models.Evaluation

<h2>Details</h2>
<h2>Evaluation</h2>

@using (Html.BeginForm())
{
    @Html.EditorFor(x => x.Questions)
    <input type="submit" />
}

评估.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.ComponentModel.DataAnnotations;


namespace JavaTutorial.Models
{
    public class Evaluation
    {
        public List<Question> Questions { set; get; }
        public Evaluation()
        {
            Questions = new List<Question>();
        }
    }

}

测验.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace JavaTutorial.Models
{
    public class Quiz
    {
        [Key]
        public int QuizId { get; set; }
        public string Title { get; set; }
        public int difficulty { get; set; }
        public string description { get; set; }
        public Genre Genre { get; set; }
    }

    public class Question
    {
        public int ID { set; get; }
        public string QuestionText { set; get; }
        public List<Answer> Answers { set; get; }
        [Required]
        public string SelectedAnswer { set; get; }
        public Question()
        {
            Answers = new List<Answer>();
        }
    }
    public class Answer
    {
        public int ID { set; get; }
        public string AnswerText { set; get; }
    }
   /* public class Evaluation
    {
        public List<Question> Questions { set; get; }
        public Evaluation()
        {
            Questions = new List<Question>();
        }
    }*/

    /*public class ParentView
    {
        public Quiz Quiz { get; set; }
        public Question Question { get; set; }
        public Answer Answer { get; set; }
        public Evaluation Evaluation{ get; set; }

    }
    */


    /*
     * using (Html.BeginForm())
{
    @Html.EditorFor(x => x.Evaluation.Questions)
    <input type="submit" />
}*/
}

控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JavaTutorial.Models;
//using ViewModels;


namespace JavaTutorial.Controllers
{
    public class QuizController : Controller
    {
        //
        // GET: /Quiz/

        TutorialEntities storeDB = new TutorialEntities();



        public ActionResult Details(int id)
        {
            var quiz = storeDB.Quizzes.Find(id);
            return View(quiz);
        }

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4 razor dictionary


    【解决方案1】:

    您已在视图中设置模型:

    @model JavaTutorial.Models.Evaluation
    

    但你没有通过这个模型:

    JavaTutorial.Models.Quiz
    

    View 需要模型类 Evaluation,但您正在通过 Quiz

    如果你想像这样在视图中通过测验模型更改:

    @model JavaTutorial.Models.Quiz
    

    如果您想传递两个模型,请创建一个 viewmodel 并将其放在解决方案中名为 ViewModels 的文件夹中。

    public class MyViewModel
    {
    public List<Question> Questions { set; get; }
    public List<Quiz> Quizzes{ set; get; }
    public List<Answer> Asnwers { set; get; }
    }
    

    ans 将视图模型传递给您的视图并在视图中设置如下:

    @model JavaTutorial.ViewModels.MyViewModel
    

    【讨论】:

    • 我只是想通过评估,为什么要找测验?
    • 因为您将测验对象传递给查看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多