2014-12-30 18 views
5

Tôi đang cố chuyển giá trị nút radio đã chọn này từ chế độ xem dao cạo đến bộ điều khiển .... ai đó có thể cho tôi ý tưởng cách thực hiện việc này? ..................... Chế độ xem của tôi trông giống như sau:Truyền giá trị đã chọn từ các nút radio đến bộ điều khiển trong MVC

@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" /> 
} 

Trình điều khiển của tôi trông như sau: .........

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




    } 

Đây là những phương pháp khác:

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

Cảm ơn bạn !!!!

+0

Tạo các dấu hiệu ẩn cho mọi câu hỏi. – Mairaj

Trả lời

10

Thêm bất động sản:

public string SelectedAnswer { get; set; } 

Thêm theo quan điểm:

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

Trong điều khiển nó postback giá trị theo nút radio được chọn. .. tức là Answer1 hoặc Answer2, v.v.

1

Hoặc tạo ra một lĩnh vực tiềm ẩn cho mỗi nút radio hoặc thay đổi bạn nút radio như thế này

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

ahhh được rồi, tôi sẽ thử ... – Dayan

4

Nếu bạn đang đưa ra cùng một tên cho nút radio của bạn, sau đó bạn có thể thử các mã sau

điều khiển

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

Xem

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

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

bộ sưu tập sử dụng hình thức

[HttpPost] 
    public ActionResult Index(FormCollection fc) 
    { 

     string answerA = fc["Answer1"]; 
     string answerB = fc["Answer2"]; 
     string answerC = fc["Answer3"]; 
     string answerD = fc["Answer4"]; 
     return View(); 
    } 
Các vấn đề liên quan