我正在尝试将此选定的单选按钮值从剃刀视图传递给控制器.... 有谁可以请你知道如何做到这一点?..................... 我的观点如下:
@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" />
}
我的控制器看起来像这样:.........
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;
}
谢谢!!!!
答案 0 :(得分:14)
添加属性:
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等。
答案 1 :(得分:4)
如果您为单选按钮指定相同名称,则可以尝试以下代码
<强>控制器强>
[HttpPost]
public ActionResult Index(string Answer)
{
return 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" />
}
答案 2 :(得分:2)
为每个单选按钮创建一个隐藏字段或更改像这样的单选按钮
@Html.RadioButtonFor("Answer1", new { @id = 1 })
答案 3 :(得分:1)
使用表单集
[HttpPost]
public ActionResult Index(FormCollection fc)
{
string answerA = fc["Answer1"];
string answerB = fc["Answer2"];
string answerC = fc["Answer3"];
string answerD = fc["Answer4"];
return View();
}
答案 4 :(得分:1)
将以下源代码添加到View:
@Html.RadioButtonFor(m => m.Role, "1" ) <span>A</span>
@Html.RadioButtonFor(m => m.Role, "0" ) <span>B</span>
@Html.HiddenFor(m=>m.Role)
您可以将选定的值放入此HiddenField:@Html.HiddenFor(m=>m.Role)
,然后通过[HttpPost]原型将其发送到控制器。
答案 5 :(得分:0)
我在另一个线程上看到了它,它像冠军一样工作。
像这样的剃刀视图:
@using (Html.BeginForm("test", "testcontroller"))
{
@Html.RadioButton("CashPayment", "1")<span>Yes</span>
@Html.RadioButton("CashPayment", "0")<span>No</span>
}
和像这样的控制器:
public ActionResult test(string CashPayment)
{
//your code
}