Asp.net mvc 将复选框连接到查询作为值的筛选条件
我是微软MVC的新手,我有这个问题。我尝试用一些复选框过滤数据库中的数据。 我使用的是微软MVC。LINQ to SQL,MS SQL ExpressAsp.net mvc 将复选框连接到查询作为值的筛选条件,asp.net-mvc,checkbox,Asp.net Mvc,Checkbox,我是微软MVC的新手,我有这个问题。我尝试用一些复选框过滤数据库中的数据。 我使用的是微软MVC。LINQ to SQL,MS SQL Express <% using (Html.BeginForm()) { %> <fieldset> <legend>Search filter</legend> <fieldset style="width:130px;height:150px;float
<% using (Html.BeginForm())
{ %>
<fieldset>
<legend>Search filter</legend>
<fieldset style="width:130px;height:150px;float:left;margin-left:10px">
<legend></legend>
<table>
<tr><td><%= Html.CheckBox("checkbox", false)%></td><td>Solar</td></tr>
<tr><td><%= Html.CheckBox("checkbox1", false)%></td><td>Water</td></tr>
<tr><td><%= Html.CheckBox("checkbox2", false)%></td><td>Biomas</td></tr>
<tr><td><%= Html.CheckBox("checkbox3", false)%></td><td>Other....</td></tr>
</table>
</fieldset>
在view.aspx中收集和查看数据
<%foreach (RenewalSourcesWeb.Models.TypeDetail solar in (List<RenewalSourcesWeb.Models.TypeDetail>) ViewData["solar"]) {%>
<table>
<tr><td>Value1</td><td><%=solar.Description %></td></tr>
<tr><td>Value2</td><td><%=solar.KindName %></td></tr>
</table>
<%} %>
价值1
价值2
我的问题是,复选框的值是我认为正确或错误没有复选框,我不能使这项工作。
如果有人提出一些建议,我将非常感谢您的帮助。此外,如果有人提出解决多个复选框的建议,那么这些建议都是正确的。以下是给您的一些建议: 1.)除非您有强制使用MVC版本2或1的要求,否则请将WebForms视图引擎()更改为Razor视图引擎(
@Syntax.LikeThis
)
2.)使用强类型ViewModel而不是ViewData
public class SearchViewModel
{
public bool checkbox { get; set; }
public bool checkbox1 { get; set; }
// ...etc
}
public ActionResult Search()
{
return View(new SearchViewModel());
}
<tr><td>@Html.CheckBoxFor(m => m.checkbox)</td><td>Solar</td></tr>
<tr><td>@Html.CheckBoxFor(m => m.checkbox1)</td><td>Water</td></tr>
[HttpPost]
public ActionResult Search(SearchViewModel model)
{
string value = model.checkbox;
string value2 = model.checkbox1;
// ...etc
谢谢你的回答,我明白你的意思。但事实上,我不得不使用MVC2,因为我的整个项目都在旧vercion中,没有使用razor引擎,我没有时间进入这项新技术并重建整个项目。无论如何,谢谢你的努力。当做George@user661597,你仍然可以使用我的建议2和3。用webforms语法替换razor语法即可。
public class SearchViewModel
{
public bool checkbox { get; set; }
public bool checkbox1 { get; set; }
// ...etc
}
public ActionResult Search()
{
return View(new SearchViewModel());
}
<tr><td>@Html.CheckBoxFor(m => m.checkbox)</td><td>Solar</td></tr>
<tr><td>@Html.CheckBoxFor(m => m.checkbox1)</td><td>Water</td></tr>
[HttpPost]
public ActionResult Search(SearchViewModel model)
{
string value = model.checkbox;
string value2 = model.checkbox1;
// ...etc
@using (Html.BeginForm("Search", "Search", FormMethod.Get))
{
...