将选定的值从单选按钮传递到 MVC 中的控制器 [英] Passing selected value from the radio buttons to the controller in MVC

查看:27
本文介绍了将选定的值从单选按钮传递到 MVC 中的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将这个选定的单选按钮值从剃刀视图传递到控制器....有人可以给我一个想法如何做到这一点吗?........................我的视图如下所示:

I'm trying to pass this selected radio button value from the razor view to the controller.... can someone please give me an idea how to do this?..................... My View looks like this:

@using (Html.BeginForm("Index", "Exam"))
{

    @Html.Hidden("qid", Model.ID, new { @id = "id" })
    <table>
        <tr>
            <td>
                @Model.ID
            </td>
            <td>
                @Model.QuestionDes
            </td>
        </tr>
        <tr>
            <td>
                <p>@Html.RadioButton("Answer1", new { @id = 1 })  @Model.Answer1 </p>
            </td>
        </tr>
        <tr>
            <td>
                <p>@Html.RadioButton("Answer2", new { @id = 2 })  @Model.Answer2 </p>
            </td>
        </tr>
        <tr>
            <td>
                <p>@Html.RadioButton("Answer3", new { @id = 3 })  @Model.Answer3 </p>
            </td>
        </tr>
        <tr>
            <td>
                <p>@Html.RadioButton("Answer4", new { @id = 4 })  @Model.Answer4 </p>
            </td>
        </tr>

    </table>

    <input type="submit" value="Next" />

}
@using (Html.BeginForm("PrevIndex", "Exam"))
{

    @Html.Hidden("qid1", Model.ID, new { @id = "id1" })
    <input value="Prev" type="submit" />
}

我的控制器看起来像这样:.........

My Controller looks like this:.........

 public class ExamController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
            IQuestionService ser = new QuestionService();
            QuestionLoadDTO q = ser.GetIndividualQuestions(1);
            return View(q);
        }

        [HttpPost]
        public ActionResult Index(QuestionLoadDTO ques)
        {
            int count = 0;
            count = int.Parse(Request["qid"].ToString());
            count++;
            if (count <= 4)
            {
                IQuestionService ser = new QuestionService();
                QuestionLoadDTO q = ser.GetIndividualQuestions(count);
                return View(q);
            }
            return RedirectToAction("Submit");

        }
        public ActionResult PrevIndex(QuestionLoadDTO ques)
        {
            int count1 = 0;
            count1 = int.Parse(Request["qid1"].ToString());
            count1--;
            if (count1 < 5 || count1 >= 0)
            {
                IQuestionService ser = new QuestionService();
                QuestionLoadDTO q = ser.GetIndividualQuestions(count1);
                return View("Index", q);
            }
            return RedirectToAction("End");

        }
        public ActionResult Submit()
        {
            return View();
        }
        public ActionResult End()
        {
            return View();
        }




    }

这些是其他方法:

QuestionLoadDTO IQuestionService.GetIndividualQuestions(int index)
{
    IQuestionData ser = new QuestionRepository();           

    QuestionLoadDTO q = ser.GetIndividualQues(index);

    return q;
}
public QuestionLoadDTO GetIndividualQues(int index)
{
    Context con = new Context();
    Question question = con.Questions.Find(index);//Where(e => e.ID == index).FirstOrDefault();
    QuestionLoadDTO dto = new QuestionLoadDTO()
    {
        ID = question.ID,
        QuestionDes = question.QuestionDes,
        Answer1 = question.Answer1,
        Answer2 = question.Answer2,
        Answer3 = question.Answer3,
        Answer4 = question.Answer4

    };                                
    return dto;
}

谢谢!!!!

推荐答案

添加属性:

public string SelectedAnswer { get; set; }

在视图中添加:

@Html.RadioButtonFor(m => m.SelectedAnswer, "Answer1")
@Html.RadioButtonFor(m => m.SelectedAnswer, "Answer2")
@Html.RadioButtonFor(m => m.SelectedAnswer, "Answer3")
@Html.RadioButtonFor(m => m.SelectedAnswer, "Answer4")

在控制器中,它根据选定的单选按钮回发值......即Answer1或Answer2等

In controller it postback the value according to selected radio button... i.e. either Answer1, or Answer2, etc.

这篇关于将选定的值从单选按钮传递到 MVC 中的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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