其中语句 MVC 4 中的可选条件
本文关键字:条件 语句 MVC | 更新日期: 2023-09-27 18:31:19
我有一个模型:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace ReviewLogs.Models
{
public class SearchModel
{
[Display(Name = "Type: ")]
public int? type { get; set; }
[Display(Name = "Server: ")]
public string curServer { get; set; }
[Display(Name = "Status: ")]
public string status { get; set; }
[Display(Name = "User Name: ")]
public string searchedUser { get; set; }
[Display(Name = "Month Ending: ")]
public int? monthEnd { get; set; }
[Display(Name = "Year Ending: "),
RegularExpression("^([0-9]{4})$", ErrorMessage = "Year must be entered and a valid 4 digit number")]
public int? yearEnd { get; set; }
[Display(Name = "Min Log Date: ")]
public string minLogDate { get; set; }
[Display(Name = "Max Log Date: ")]
public string maxLogDate { get; set; }
[Display(Name = "Ticket Number: ")]
public string ticket { get; set; }
[Display(Name = "Comment: ")]
public string comment { get; set; }
public SearchModel()
{
searchedUser = " ";
}
}
}
A 控制器:
public ActionResult LogSearch()
{
if (Request.IsAuthenticated)
{
loadDropDown();
return View();
}
else
{
return RedirectToAction("index", "home");
}
}
[HttpPost]
public ActionResult LogSearch(SearchModel submission)
{
loadDropDown();
return RedirectToAction("NameSearchList", "Search", new { userName = submission.searchedUser ,type = submission.type.Value});
}
public ActionResult NameSearchList(int? type, string userName = "")
{
var cnn = new reviewlogsEntities();
return View(cnn.logrecords.Where(m => m.username == userName && m.typeid == type));
}
和观点:
@model ReviewLogs.Models.SearchModel
@{
ViewBag.Title = "Add Review";
}
@section Scripts{
<script src="~/Scripts/jquery-1.12.0.min.js"></script>
<script src="~/Scripts/datetimepicker_css.js"></script>
<script src="~/Scripts/CurDropDownChecker.js"></script>
}
}
<h2>Add Review</h2>
@Html.ValidationSummary(true, "Submission Failed, Please Try Again")
<p id="success">@ViewBag.SuccessMessage</p>
<div class="formBody">
@using (Html.BeginForm())
{
<div class="inputOthers">
@Html.LabelFor(model => model.type)
@Html.DropDownList("type", ViewBag.type as IEnumerable<SelectListItem>, "", new { onchange = "setDisplay();" })
@Html.ValidationMessageFor(model => model.type)
</div>
<div class="inputOthers">
@Html.LabelFor(model => model.searchedUser)
@Html.DropDownList("searchedUser", ViewBag.users as IEnumerable<SelectListItem>, " ")
</div>
<div class="inputOthers">
@Html.LabelFor(model => model.curServer)
@Html.DropDownList("curServer", ViewBag.server as IEnumerable<SelectListItem>, " ")
</div>
<div class="inputOthers">
@Html.LabelFor(model => model.status)
@Html.DropDownList("status", ViewBag.status as IEnumerable<SelectListItem>, " ")
</div>
<div class="inputOthers">
@Html.LabelFor(model => model.ticket)
@Html.TextBoxFor(model => model.ticket)
</div>
<div class="inputOthers">
@Html.LabelFor(model => model.comment)
@Html.TextAreaFor(model => model.comment, new { style = "width:320px; height: 160px;" })
</div>
<div class="inputOthers">
@Html.LabelFor(model => model.minLogDate)
@Html.TextBoxFor(model => model.minLogDate, new { @readonly = "readonly" })
<a href="javascript: NewCssCal('logDate','mmddyyyy','arrow',true,12)">
<img src="~/Images/cal.gif" width="16" height="16" alt="Pick a date">
</a>
</div>
<div class="inputOthers">
@Html.LabelFor(model => model.maxLogDate)
@Html.TextBoxFor(model => model.maxLogDate, new { @readonly = "readonly" })
<a href="javascript: NewCssCal('logDate','mmddyyyy','arrow',true,12)">
<img src="~/Images/cal.gif" width="16" height="16" alt="Pick a date">
</a>
</div>
<div class="inputOthers">
<span id="monthEnd">
@Html.LabelFor(model => model.monthEnd)
@Html.DropDownListFor(model => model.monthEnd, ViewBag.months as IEnumerable<SelectListItem>, " ")
</span>
<span id="yearEnd">
@Html.LabelFor(model => model.yearEnd)
@Html.TextBoxFor(model => model.yearEnd, new { @Value = DateTime.Now.Year })
</span>
</div>
<input class="submitButton" type="submit" value="Submit" style="margin-left:126px;margin-bottom: 20px;" onclick="return confirm('If you would like to submit press OK');" />
}
</div>
因此,如果您查看此内容,则用户可以输入许多可选字段。如果他们在该字段中输入数据,则将其添加到搜索结果中。现在,如果他们输入类型并搜索用户,它将调出包含相应数据的页面。但是,我试图解决的问题是如果他们不输入类型怎么办。我不希望出现这种搜索。现在它这样做:
http://localhost:51730/Search/NameSearchList?userName=Admin&type=1
但是假设他们不搜索名称,而只搜索类型。如何确保"用户名=管理员"不会出现。我想限制每一个,如果它们没有输入或 NULL,只有输入的那些。
您能否展示一下我将如何制定可选要求的解决方案?
谢谢!
有两个选项可用于在 NameSearchList 操作中添加忽略空值的条件筛选器
选项 1:检查 where 语句中的空值
var q = cnn.logrecords.Where(m => (m.username == userName || string.IsNullOrEmpty(userName)) && (m.typeid == type || !type.HasValue))
return View(q);
选项 2:仅为那些非空参数添加 where 语句
var q = cnn.logrecords;
if(!string.IsNullOrEmpty(userName))
{
q = q.Where(m => m.username == userName);
}
if(type.HasValue)
{
q = q.Where(m => m.typeid == type);
}
return View(q);