如何从html表单获取数据并将其存储在ASP.Net MVC视图中的c#变量中 [英] how to get data from html form and store it in a c# variable in ASP.Net MVC view

查看:65
本文介绍了如何从html表单获取数据并将其存储在ASP.Net MVC视图中的c#变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手.我对HTML,Css,C#,SQL查询,实体框架和ASP.Net-MVC有基本的了解.我正在尝试在ASP.Net-MVC中进行小型时间表测试.

I'm new to programming. I have basic understanding of Html, Css, C#, SQL queries, Entity Framework and ASP.Net-MVC. I'm trying to make a small time table test in ASP.Net-MVC.

此应用程序的目的是要问10个问题,一次问一个问题,并且每次尝试提问时,用户都将单击"Next"(下一步)按钮,该按钮将显示下一组问题.用户需要尝试所有10个问题.到达后,它会告诉显示一条消息,例如恭喜,您已经回答了10个问题中的7个."

The purpose of this application is to ask 10 questions, one question at a time and each time a question is attempted user will click on next button that will show next set of question. User need to attempt all 10 questions. Once reached it will tell show a message e.g. "Congratulations You have answered 7 questions From 10".

我想知道是否有可能从html表单中获取数据,然后将其存储在ASP.Net MVC视图中的C#变量中.以下是我到目前为止尝试过的内容:

I want to know if it is possible to get data from html form and then store it in C# variable in ASP.Net MVC View. Below is what I have tried so far:

问题视图

@{ 
Random rnd = new Random();
int num1 = rnd.Next(13);
int num2 = rnd.Next(13);
int answer = num1 * num2;
int wrng1 = answer - 2;
int wrng2 = answer + 10;
int wrng3 = answer + 4;
int QAnswered = 0;
}

<div style="text-align:center;">
<h1>Questions Answered: @QAnswered</h1>

<!--Writes the Question-->
<h2 style="font-size: 150px; font-weight:900;">@num1 X @num2 = ?</h2>

<!--Buttons for Answers in form-->
<form action="/Home/CheckAnswer" method="post">
    <input type="radio" name="UserAnswer" value="@answer" style="margin-right: 10px; width: 50px; height: 50px;" /><label style="color:white; font-size: 75px; width: 150px; height: 100px; border:1px solid black; background-color: cornflowerblue;">@answer</label>
    <input type="radio" name="UserAnswer" value="@wrng1" style="margin-right: 10px; width: 50px; height: 50px;" /><label style="color:white; font-size: 75px; width: 150px; height: 100px; border:1px solid black; background-color: cornflowerblue;">@wrng1</label>
    <br /><br /><br />
    <input type="radio" name="UserAnswer" value="@wrng2" style="margin-right: 10px; width: 50px; height: 50px;" /><label style="color:white; font-size: 75px; width: 150px; height: 100px; border:1px solid black; background-color: cornflowerblue;">@wrng2</label>
    <input type="radio" name="UserAnswer" value="@wrng3" style="margin-right: 10px; width: 50px; height: 50px;" /><label style="color:white; font-size: 75px; width: 150px; height: 100px; border:1px solid black; background-color: cornflowerblue;">@wrng3</label>
    <br /><br />
    <input type="hidden" name="CorrectAnswer" value="@answer" />
    <input type="hidden" name="QAnswered" value="@QAnswered" />
    <input class="btn btn-primary" type="submit" value="Next" style="font-size: 50px; width: 150px; height: 75px;" />
</form>

这是家庭控制器中动作"的代码:

and this is the code for the Actions in home controller:

public ActionResult Question()
    {
        return View();
    }

    [HttpPost]
    public ActionResult CheckAnswer(int UserAnswer, int CorrectAnswer, int QAnswered)
    {   
        int TrueAnswers = 0;
        int FalseAnswers = 0;         
        //Check whether UserAnswer is true or false and increment values of them
        if (UserAnswer == CorrectAnswer)
        {
            TrueAnswers += 1;
            QAnswered += 1;
        }
        else if (UserAnswer != CorrectAnswer)
        {
            FalseAnswers += 1;
            QAnswered += 1;
        }

        if (QAnswered < 10)
        {
            return RedirectToAction("Question");
        }
        else
        {
            return RedirectToAction("Index");
        }
    }

问题是我不想让CheckAnswer动作返回内容,而是希望它存储UserAnswer并检查它是否正确,然后它将(以某种方式)存储为true,反之亦然,并在出现10个问题之后(无论是对还是错)我希望它显示另一个视图,该视图将显示类似恭喜,您已正确回答10个问题中的7个".

The thing is I don't want to make CheckAnswer action return content instead I want it to store the UserAnswer and check if it was correct then it will (somehow) store it as a true and vice versa and and after 10 questions (whether if it was right or wrong) I want it to display another View Which Will say something like "Congratulations you have answered 7 out of 10 questions correctly".

我不知道该怎么做,所以我希望有人能帮助我.

I don't know how will I do it so I hope anyone Will Help Me.

推荐答案

返回Content("...")仅用于 return TEXT-Plain FORMAT值

return Content("...") is only to return TEXT-Plain FORMAT value

如果要在提交表单后触发其他操作,则应输入 return RedirectToAction("YOUR-ACTION")

If you want to fire another action after form submit, you should enter return RedirectToAction("YOUR-ACTION")

更新

控制器:

[HttpGet]
public ActionResult Question()
    {
        Random rnd = new Random();
        int num1 = rnd.Next(13);
        int num2 = rnd.Next(13);
        return View(new QuestionViewModel
        {
            QuestionPart1 = num1,
            QuestionPart2 = num2,
        });
    }

    [HttpPost]
    public ActionResult Question(QuestionViewModel model)
    {
        //Check whether UserAnswer is true or false and increment values of them
        if (model.UserAnswer == (model.QuestionPart1 * model.QuestionPart2))
        {
            if (Session["QAnswered"] == null)
                Session["QAnswered"] = 1;
            else
                Session["QAnswered"] = int.Parse(Session["QAnswered"].ToString()) + 1;
        }
        else
        {
            if (Session["Failed"] == null)
                Session["Failed"] = 1;
            else
                Session["Failed"] = int.Parse(Session["Failed"].ToString()) + 1;
        }

        if (Session["QAnswered"] != null && int.Parse(Session["QAnswered"].ToString()) == 10)
        {
            // successful
            return RedirectToAction("Congratulation");
        }
        else
        {
            // failed
            return RedirectToAction("Question");
        }
    }

查看:

    @model WebApplication2.Models.QuestionViewModel
    @{
        int answer = Model.QuestionPart1 * Model.QuestionPart2;
        int wrng1 = answer - 2;
        int wrng2 = answer + 10;
        int wrng3 = answer + 4;
        var answers = new List<int> { answer, wrng1, wrng2, wrng3 };
        Random rnd = new Random();
        answers = answers.OrderBy(x => rnd.Next()).ToList();
    }
    <div style="text-align:center;">
        <h1>Questions Answered: @Session["QAnswered"]</h1>
    @if (int.Parse(Session["QAnswered"].ToString()) >= 7){
<text>
Congratulations, you've answered 7 of 10.
</text>
}
        <!--Writes the Question-->
        <h2 style="font-size: 150px; font-weight:900;">@Model.QuestionPart1 X @Model.QuestionPart2 = ?</h2>

        <!--Buttons for Answers in form-->
        @using (Html.BeginForm("Question", "Home", FormMethod.Post))
        {
            for (int index = 0; index < 4; index++)
            {
                var choice = answers[index];
                <input type="radio" id="UserAnswer" name="UserAnswer" value="@choice" style="margin-right: 10px; width: 50px; height: 50px;" />
                <label style="color:white; font-size: 75px; width: 150px; height: 100px; border:1px solid black; background-color: cornflowerblue;">@choice</label>
                <br />

            }

            @Html.HiddenFor(model => model.QuestionPart2)
            @Html.HiddenFor(model => model.QuestionPart1)
            <input class="btn btn-primary" type="submit" value="Next" style="font-size: 50px; width: 150px; height: 75px;" />

        }
    </div>

型号:

public class QuestionViewModel
    {
        public int QuestionPart1 { get; set; }
        public int QuestionPart2 { get; set; }
        public int UserAnswer { get; set; }
    }

这篇关于如何从html表单获取数据并将其存储在ASP.Net MVC视图中的c#变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆