Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在MVC中使用复选框查询所选项目_C#_Asp.net Mvc_Linq_Checkbox - Fatal编程技术网

C# 在MVC中使用复选框查询所选项目

C# 在MVC中使用复选框查询所选项目,c#,asp.net-mvc,linq,checkbox,C#,Asp.net Mvc,Linq,Checkbox,我想问一下,在MVC中进行linq查询时,如何使用复选框中的选定项。 在我的视图中,我显示了所有可能的选项,用户只需选择用于生成报告的软件类型 @using (Ajax.BeginForm("Create_Report", "Softwares", new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.

我想问一下,在MVC中进行linq查询时,如何使用复选框中的选定项。 在我的视图中,我显示了所有可能的选项,用户只需选择用于生成报告的软件类型

@using (Ajax.BeginForm("Create_Report", "Softwares",
            new AjaxOptions
            {
                HttpMethod = "POST",
                InsertionMode = InsertionMode.Replace,
                UpdateTargetId = "target2"
            })) 
        { 
            @Html.ValidationSummary(true)
             <p>For A Reports: Kindly check softwares to create reports.</p><br />
            foreach (var item in Model) { 
                <input type="checkbox" value="@item.software_name" name="software_type"/>@item.software_name
                <br />
            }


            <input type="submit" value="Create Report"/>

        }
@使用(Ajax.BeginForm(“创建报告”、“软件”),
新选择
{
HttpMethod=“POST”,
InsertionMode=InsertionMode.Replace,
UpdateTargetId=“target2”
})) 
{ 
@Html.ValidationSummary(true)
对于报告:请检查软件以创建报告。


@using (Ajax.BeginForm("Create_Report", "Softwares",
            new AjaxOptions
            {
                HttpMethod = "POST",
                InsertionMode = InsertionMode.Replace,
                UpdateTargetId = "target2"
            })) 
        { 
            @Html.ValidationSummary(true)
             <p>For A Reports: Kindly check softwares to create reports.</p><br />
            foreach (var item in Model) { 
                <input type="checkbox" value="@item.software_name" name="software_type"/>@item.software_name
                <br />
            }


            <input type="submit" value="Create Report"/>

        }
foreach(模型中的变量项){ @item.software\u名称
} }
之后,我希望在查询中使用所选的软件类型,例如,如果用户选择Adobe Pro、Adobe Illustrator、MS Visio和Acrobat,则查询应类似于“从软件表中选择软件”,其中software_utype=“Adobe Pro”&&software_utype=“Adobe Illustrator&&”


是否有任何方法可以使用复选框中的选定项缩短查询?非常感谢您的帮助。

您需要定义一个集合来存储选定项,并使用模型绑定将选定项映射到此属性

//store checked Items
public IEnumerable<string> SelectedSoftware { get; set; }
//check box list items
public List<string> SoftwareList = new List<string>();

 foreach (var item in Model.SoftwareList )
    {
        <input type="checkbox" name="SelectedSoftware" value="@item">@item

    }

假设您的POST方法是

[HttpPost]
public ActionResult Create_Report(string[] software_type)
然后,
software\u type
将包含所选值的数组,因此您可以使用
.Contains()
过滤查询

var items = db.Software.Where(s => software_type.Contains(s.software_type))

旁注:从属性中删除那些可怕的下划线,并遵循正常的命名约定(SoftwareType、SoftwareName等,或者简单地键入、命名等,因为它们已经在名为Software的类中)

您正在发布所选项目,对吗?只需显示控制器操作,而不是htmlI在MVC中是新的。我想问一下我如何将所选项目传递给控制器。我是否应该将其放入数组中,然后在查询中使用它?显示您发布到的方法-它应该具有参数<代码>(字符串[]软件类型)。这是我想问的问题。我仍然不知道如何在linq查询中传递数组。在查询过程中会发生什么?非常感谢@Stephen。太好了!它工作了!