Tham số đầu tiên của Html.RadioButtonFor() phải là tên thuộc tính bạn đang sử dụng và tham số thứ hai phải là giá trị của nút radio cụ thể đó. Sau đó, chúng sẽ có cùng giá trị thuộc tính name và helper sẽ chọn nút radio đã cho khi/nếu nó khớp với giá trị thuộc tính.
Ví dụ:
<div class="editor-field">
<%= Html.RadioButtonFor(m => m.Gender, "M") %> Male
<%= Html.RadioButtonFor(m => m.Gender, "F") %> Female
</div>
Dưới đây là một ví dụ cụ thể hơn:
tôi đã thực hiện một dự án MVC nhanh chóng có tên là "DeleteMeQuestion" (DeleteMe tiền tố vì vậy tôi biết rằng tôi có thể loại bỏ nó sau này sau khi Tôi quên nó đi.
tôi đã thực hiện mô hình sau:
namespace DeleteMeQuestion.Models
{
public class QuizModel
{
public int ParentQuestionId { get; set; }
public int QuestionId { get; set; }
public string QuestionDisplayText { get; set; }
public List<Response> Responses { get; set; }
[Range(1,999, ErrorMessage = "Please choose a response.")]
public int SelectedResponse { get; set; }
}
public class Response
{
public int ResponseId { get; set; }
public int ChildQuestionId { get; set; }
public string ResponseDisplayText { get; set; }
}
}
Có một validator phạm vi đơn giản trong mô hình, chỉ để cho vui. Tiếp theo, tôi đã thực hiện bộ điều khiển sau:
namespace DeleteMeQuestion.Controllers
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index(int? id)
{
// TODO: get question to show based on method parameter
var model = GetModel(id);
return View(model);
}
[HttpPost]
public ActionResult Index(int? id, QuizModel model)
{
if (!ModelState.IsValid)
{
var freshModel = GetModel(id);
return View(freshModel);
}
// TODO: save selected answer in database
// TODO: get next question based on selected answer (hard coded to 999 for now)
var nextQuestionId = 999;
return RedirectToAction("Index", "Home", new {id = nextQuestionId});
}
private QuizModel GetModel(int? questionId)
{
// just a stub, in lieu of a database
var model = new QuizModel
{
QuestionDisplayText = questionId.HasValue ? "And so on..." : "What is your favorite color?",
QuestionId = 1,
Responses = new List<Response>
{
new Response
{
ChildQuestionId = 2,
ResponseId = 1,
ResponseDisplayText = "Red"
},
new Response
{
ChildQuestionId = 3,
ResponseId = 2,
ResponseDisplayText = "Blue"
},
new Response
{
ChildQuestionId = 4,
ResponseId = 3,
ResponseDisplayText = "Green"
},
}
};
return model;
}
}
}
Cuối cùng, tôi đã nhìn sau mà làm cho việc sử dụng mô hình:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<DeleteMeQuestion.Models.QuizModel>" %>
<asp:Content ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content>
<asp:Content ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm()) { %>
<div>
<h1><%: Model.QuestionDisplayText %></h1>
<div>
<ul>
<% foreach (var item in Model.Responses) { %>
<li>
<%= Html.RadioButtonFor(m => m.SelectedResponse, item.ResponseId, new {id="Response" + item.ResponseId}) %>
<label for="Response<%: item.ResponseId %>"><%: item.ResponseDisplayText %></label>
</li>
<% } %>
</ul>
<%= Html.ValidationMessageFor(m => m.SelectedResponse) %>
</div>
<input type="submit" value="Submit" />
<% } %>
</asp:Content>
Như tôi hiểu hoàn cảnh của bạn, bạn có thắc mắc với một danh sách câu trả lời có sẵn. Mỗi câu trả lời sẽ quyết định câu hỏi tiếp theo. Hy vọng rằng có ý nghĩa từ mô hình của tôi và nhận xét TODO.
Nút này cung cấp cho bạn các nút radio có cùng thuộc tính tên nhưng thuộc tính ID khác nhau.
Tại sao bạn không thể sử dụng 'Html.RadioButton' thay thế? – Ahmad
Vì đây là chế độ xem mạnh mẽ nên tôi đang cố gắng tạo ra dữ liệu để dữ liệu trở lại được điền vào bộ điều khiển. Tôi đang mở cho bất kỳ đề xuất nào! –