簡體   English   中英

ASP.Net MVC隨機,控制器和視圖

[英]ASP.Net MVC random, Controllers and views

我在創建ASP.NET MVC程序時遇到了麻煩,該程序將隨機選擇一個學生並隨機選擇一個問題,由於某種原因,我無法在.cshtml文件中使用它。 難道我做錯了什么? 我也得到這個錯誤

使用QuizProgramMVC.Models,“傳遞到字典中的模型項的類型為'QuizProgramMVC.Models.Student',但是此字典要求模型項的類型為'QuizProgramMVC.Models.StudentQuestion'。”

控制者

public class QuizController : Controller
{
    public QuizController TheQuiz { get; set; }

    // GET: Quiz
    public ActionResult Index()
    {
        StudentQuestion sq = new StudentQuestion();

        return View(sq);
    }

    public ActionResult QuizProgram()
    {        
        Student program = new Student();

        return View(program);
    }

}

視圖

@model QuizProgramMVC.Models.StudentQuestion
@using QuizProgramMVC.Models;

@{

    Layout = null;
 }

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>QuizProgram</title>
</head>
<body>
    <div> 
        <h1>Quiz Program</h1>

                <div class="row">
                    <div class="col-md-6">
                        <table class="table table-condensed">
                            <thead>
                                <tr>
                                    <th>Question</th>
                                    <th>Answer</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach (Question q in Model.Questions)
                                {
                                    <tr>
                                        <td>
                                            @q.Quest
                                        </td>
                                        <td>
                                            @q.Answer
                                        </td>
                                    </tr>
                                }
                            </tbody>
                        </table>
                    </div>
                </div>
            <br/>
                <div class="row">
                    <div class="col-md-6">
                        <table class="table table-condensed">
                            <thead>
                                <tr>
                                    <th>First Name</th>
                                    <th>Last Name</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach (Student s in Model.Students)
                                {
                                    <tr>
                                        <td>
                                            @s.FirstName
                                        </td>
                                        <td>
                                            @s.LastName
                                        </td>
                                    </tr>
                                }
                            </tbody>
                        </table>
                    </div>
                </div>
        <br/>
        @using (Html.BeginForm())
        {
            <div>
                Type Answer: <input id="param1" name="param1" type="text" />
                <br />
                <input type="submit" value="Submit" />
            </div>
        }
    </div>
</body>
</html>

問題是您正在使用不同的模型調用同一視圖頁面。

public ActionResult QuizProgram()
{        
    Student program = new Student();//Here you need to pass the StudentQuestion to the view.
    return View(program);
}

通過QuizProgram()調用上述視圖頁面(.cshtml)會引發錯誤,因為在該視圖頁面中您將模型定義為

@model QuizProgramMVC.Models.StudentQuestion

它不允許使用其他ViewModel,因為它是強類型的 View。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM