我们如何在 View .cshtml 页面中使用 @Html.RadioButton ?
How do we use @Html.RadioButton in our View .cshtml page?
我已经成功地将 Contoso University ASP.NET MVC 项目集成到我的 VS 解决方案中。我想添加某种事件处理功能,我可以 select 索引页面上的一个或多个项目,例如,点击一个按钮并根据用户从数据库中检索某种数据 select离子。现在,我想知道如何在我点击页面上的按钮后检索用户 selected 的项目。在我们与数据库交互之前。
如何为每个项目添加正确的单选按钮,以便可以正确捕获 selected 项目?
现在我为每个项目添加了一个 @Html.RadioButton()
。但是,我不知道应该把什么作为论据?
如何添加按钮来检索 selected 数据??
我将事件处理逻辑放在哪里以及放什么来检索哪些项目已被 selected?
Views/Course/Index.cshtml
@model IEnumerable<ContosoUniversity.Models.Course>
@{
ViewBag.Title = "Courses";
}
<h2>Courses</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm())
{
<p>
Select Department: @Html.DropDownList("SelectedDepartment", "All")
<input type="submit" value="Filter" />
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.CourseID)
</th>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Credits)
</th>
<th>
Department
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.RadioButton(courseRadio, )
</td>
<td>
@Html.DisplayFor(modelItem => item.CourseID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Credits)
</td>
<td>
@Html.DisplayFor(modelItem => item.Department.Name)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.CourseID }) |
@Html.ActionLink("Details", "Details", new { id = item.CourseID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.CourseID })
</td>
</tr>
}
</table>
}
CourseController.cs:
....
public ActionResult Index(int? SelectedDepartment)
{
var departments = db.Departments.OrderBy(q => q.Name).ToList();
ViewBag.SelectedDepartment = new SelectList(departments, "DepartmentID", "Name", SelectedDepartment);
int departmentID = SelectedDepartment.GetValueOrDefault();
IQueryable<Course> courses = db.Courses
.Where(c => !SelectedDepartment.HasValue || c.DepartmentID == departmentID)
.OrderBy(d => d.CourseID)
.Include(d => d.Department);
var sql = courses.ToString();
return View(courses.ToList());
}
...
如果您的课程实体中有一个属性,例如 coursadio,
@Html.RadioButtonFor(modelitem => item.courseradio,<value you want to send to controller>)
举个例子
如果你想使用单选按钮发送男性或女性
@model HotelReservationSystem.Models.User
@using (Html.BeginForm("Action","Controller"))
{
@Html.RadioButtonFor(model => model.gender,"Male", new { id = "male" })
@Html.Label("male", "Male")
@Html.RadioButtonFor(model => model.gender,"Female", new { id = "female" })
@Html.Label("female", "Female")
@Html.ValidationMessageFor(model => model.gender, "", new { @class = "red-text" })
<input type="submit" class="btn" value="OK" />
}
public class ContactModel
{
public string Name { get; set; }
public string Gender { get; set; }
public string PhoneNumber { get; set; }
}
<div class="col-md-10">
Male
@Html.RadioButtonFor(model => model.Gender, "Male")
Female
@Html.RadioButtonFor(model => model.Gender, "Female")
</div>
参考文献:https://www.c-sharpcorner.com/article/radiobutton-in-asp-net-mvc/
我已经成功地将 Contoso University ASP.NET MVC 项目集成到我的 VS 解决方案中。我想添加某种事件处理功能,我可以 select 索引页面上的一个或多个项目,例如,点击一个按钮并根据用户从数据库中检索某种数据 select离子。现在,我想知道如何在我点击页面上的按钮后检索用户 selected 的项目。在我们与数据库交互之前。
如何为每个项目添加正确的单选按钮,以便可以正确捕获 selected 项目?
现在我为每个项目添加了一个 @Html.RadioButton()
。但是,我不知道应该把什么作为论据?
如何添加按钮来检索 selected 数据?? 我将事件处理逻辑放在哪里以及放什么来检索哪些项目已被 selected?
Views/Course/Index.cshtml
@model IEnumerable<ContosoUniversity.Models.Course>
@{
ViewBag.Title = "Courses";
}
<h2>Courses</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm())
{
<p>
Select Department: @Html.DropDownList("SelectedDepartment", "All")
<input type="submit" value="Filter" />
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.CourseID)
</th>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Credits)
</th>
<th>
Department
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.RadioButton(courseRadio, )
</td>
<td>
@Html.DisplayFor(modelItem => item.CourseID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Credits)
</td>
<td>
@Html.DisplayFor(modelItem => item.Department.Name)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.CourseID }) |
@Html.ActionLink("Details", "Details", new { id = item.CourseID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.CourseID })
</td>
</tr>
}
</table>
}
CourseController.cs:
....
public ActionResult Index(int? SelectedDepartment)
{
var departments = db.Departments.OrderBy(q => q.Name).ToList();
ViewBag.SelectedDepartment = new SelectList(departments, "DepartmentID", "Name", SelectedDepartment);
int departmentID = SelectedDepartment.GetValueOrDefault();
IQueryable<Course> courses = db.Courses
.Where(c => !SelectedDepartment.HasValue || c.DepartmentID == departmentID)
.OrderBy(d => d.CourseID)
.Include(d => d.Department);
var sql = courses.ToString();
return View(courses.ToList());
}
...
如果您的课程实体中有一个属性,例如 coursadio,
@Html.RadioButtonFor(modelitem => item.courseradio,<value you want to send to controller>)
举个例子 如果你想使用单选按钮发送男性或女性
@model HotelReservationSystem.Models.User
@using (Html.BeginForm("Action","Controller"))
{
@Html.RadioButtonFor(model => model.gender,"Male", new { id = "male" })
@Html.Label("male", "Male")
@Html.RadioButtonFor(model => model.gender,"Female", new { id = "female" })
@Html.Label("female", "Female")
@Html.ValidationMessageFor(model => model.gender, "", new { @class = "red-text" })
<input type="submit" class="btn" value="OK" />
}
public class ContactModel
{
public string Name { get; set; }
public string Gender { get; set; }
public string PhoneNumber { get; set; }
}
<div class="col-md-10">
Male
@Html.RadioButtonFor(model => model.Gender, "Male")
Female
@Html.RadioButtonFor(model => model.Gender, "Female")
</div>
参考文献:https://www.c-sharpcorner.com/article/radiobutton-in-asp-net-mvc/