获取从视图到控制器的单选按钮值 - MVC

时间:2018-02-02 03:49:53

标签: asp.net-mvc controller radio-button

我已经搜索过并找到了这个问题的相关答案,但我申请的时间非常糟糕。请原谅我,我很新。

我正在使用单选按钮进行简单的多项选择测验。在我的生活中,我不能让我的控制器从视图中的所选单选按钮读取值。我也没有使用数据库。

任何方向都会非常感激!我从来没有在这里发帖,如果我需要添加任何其他内容,请告诉我。

再次感谢您的帮助!

MODEL:

    using System.Threading.Tasks;

    namespace CommunityInfo.Models
    {
    public class QuizInfo
    {
        public string Question1 { get; set; }
        public string Question2 { get; set; }
        public string Question3 { get; set; }
        public string Question4 { get; set; }
        public string Question5 { get; set; }
    }
    }

查看:

    @model CommunityInfo.Models.QuizInfo
    @{ Layout = null; }

    <DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Quiz</title>
    <link rel="stylesheet" href="/lib/bootstrap/dist/css/bootstrap.css" />
</head>
<body>
    <form>
        <div class="panel panel-success">
            <div class="panel-heading text-center"><h4>Magic Pony Land Trivia!</h4></div>
            <div class="panel-body">
                <div>
                        <label for="q1">When was Magic Pony Sparkle Land founded?</label><br />
                        @Html.RadioButton("Question1", "a") 1 B.C. <br />
                        @Html.RadioButton("Question1", "b") 1900 <br />
                        @Html.RadioButton("Question1", "c") 2018 <br />
                    </div>
                @*<div>
                        <label for="q2">Who is the mayor of Magic Pony Sparkle Land?</label><br />
                        @Html.RadioButton("Question2", "a") Twighlight Sparkle <br />
                        @Html.RadioButton("Question2", "b") Pinkie Pie <br />
                        @Html.RadioButton("Question2", "c") Oprah <br />
                    </div>
                    <div>
                        <label for="q3">How many magic ponies inhabit Magic Pony Sparkle Land?</label><br />
                        @Html.RadioButton("Question3", "a") 4 <br />
                        @Html.RadioButton("Question3", "b") 999,999,999 <br />
                        @Html.RadioButton("Question3", "c") 500 <br />
                    </div>
                    <div>
                        <label for="q3">What is the Magic Pony Sparkle Land mascot?</label><br />
                        @Html.RadioButton("Question3", "a") Sparkly Pony (duh) <br />
                        @Html.RadioButton("Question3", "b") Sloth <br />
                        @Html.RadioButton("Question3", "c") Ostrich <br />
                    </div>*@
                <div class="text-center">
                    <button class="btn btn-primary" type="submit">
                        Submit
                    </button>
                </div>
            </div>
        </div>
    </form>
</body>

CONTROLLER

         using Microsoft.AspNetCore.Mvc;

          namespace CommunityInfo.Controllers
    {
       public class QuizController : Controller
{
    [HttpGet]
    public ViewResult Quiz()
    {
        return View();
    }

    [HttpPost]
    public ViewResult Quiz(string Question1)
    {
        if (Question1 == "a")
        {
            //do stuff
        }
        else
        {
            //do stuff
        }
    }
    }

1 个答案:

答案 0 :(得分:0)

只是在您的表单标记上遗漏了一件事以获取要发布的值:

<form method="post">