asp.net-mvc 将选定的值从单选按钮传递到 MVC 中的控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27700101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:21:22  来源:igfitidea点击:

Passing selected value from the radio buttons to the controller in MVC

asp.net-mvcasp.net-mvc-4razor

提问by Dayan

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();
        }




    }

These are the other methods:

这些是其他方法:

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;
}

Thank you!!!!

谢谢!!!!

回答by Mahesh

Add property:

添加属性:

public string SelectedAnswer { get; set; }

Add in view:

在视图中添加:

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

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

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

回答by Golda

If you are giving same name for your radio buttons, then you can try the following code

如果您为单选按钮指定相同的名称,则可以尝试以下代码

Controller

控制器

[HttpPost]
public ActionResult Index(string Answer)
{
    return View();
}

View

看法

@using (Html.BeginForm("Index", "Demo"))
{
       @Html.RadioButton("Answer", "A") <span>A</span> 
       @Html.RadioButton("Answer", "B") <span>B</span> 

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

回答by Balaji Selvarajan

Use form collection

使用表单集合

[HttpPost]
    public ActionResult Index(FormCollection fc)
    {

      string answerA = fc["Answer1"];
      string answerB = fc["Answer2"];
      string answerC = fc["Answer3"];
      string answerD = fc["Answer4"];
      return View();
    }

回答by Mairaj Ahmad

Either make a hidden field for every radio button or change you radio buttons like this

要么为每个单选按钮创建一个隐藏字段,要么像这样更改单选按钮

@Html.RadioButtonFor("Answer1", new { @id = 1 })

回答by Alexie

I saw this on another thread and it work like a champ.

我在另一个线程上看到了这个,它像冠军一样工作。

Razor view like this:

像这样的剃刀视图:

@using (Html.BeginForm("test", "testcontroller"))
{
  @Html.RadioButton("CashPayment", "1")<span>Yes</span>
  @Html.RadioButton("CashPayment", "0")<span>No</span>
}

And Controller like this:

像这样的控制器:

public ActionResult test(string CashPayment)
{   
   //your code
}

回答by Hai Dinh

Add the following source code to View:

将以下源代码添加到 View:

@Html.RadioButtonFor(m => m.Role, "1" ) <span>A</span>
@Html.RadioButtonFor(m => m.Role, "0" ) <span>B</span>
@Html.HiddenFor(m=>m.Role)

You can put the selected value to this HiddenField: @Html.HiddenFor(m=>m.Role)and send it to controller via [HttpPost] prototype.

您可以将选定的值放入此 HiddenField:@Html.HiddenFor(m=>m.Role)并通过 [HttpPost] 原型将其发送到控制器。