【发布时间】:2015-11-15 10:13:36
【问题描述】:
我有一个员工下拉列表供用户选择。当用户选择员工时,我会将值与数据库中的值进行比较。如果所选值包含员工的名字和姓氏,那么我应该能够提取他们各自的员工 ID。
我的问题是我正在使用 .Contains,它只捕获一些员工姓名,即使他们都使用相同的大小写,并且不可能出现诸如额外空格之类的错误,因为员工列表是由我与之比较的同一张表填充以查找 ID。
[HttpPost]
public ActionResult Create(AppViewModel app, App appl, string selectedEmployee)
{
//Grabs the value of the selected employee.
selectedEmployee = Request.Form["selectEmployee"].ToString();
try
{
//TODO: Add insert logic here
var emp = Database.Session.Query<Employee>().AsEnumerable();
appl.AppOwnerID = (from e in emp
where selectedEmployee.Contains(e.EmpFirstName) && selectedEmployee.Contains(e.EmpLastName)
select e.EmplID).First();
using (ITransaction transaction = Database.Session.BeginTransaction())
{
Database.Session.Save(appl);
transaction.Commit();
}
return RedirectToAction("Index");
}
catch (Exception ex)
{
return View();
}
}
由于某种原因,当我选择某些员工时,它能够找到他们的 ID,并且运行良好,但对于其他员工,它返回 " " 作为它的值,因为 Contains 方法找不到他们的名字和姓氏。
更新:这是我的列表创建和填充的地方。正如大家提到的使用 trim 时,我添加了 trim 方法,用于在列表中添加对象时,以及将其添加到我将所选值与数据库值进行比较的位置。
// GET: App/Create
public ActionResult Create(string selectEmployee)
{
//Holds all employees
var emp = Database.Session.Query<Employee>().AsEnumerable();
//Orders employees by last name.
var empOrdered = emp.OrderBy(e => e.EmpLastName);
//Formats Employees Names and creates array
var empName = (from e in empOrdered
select e.EmpFirstName + " " + e.EmpLastName).ToArray();
//List to hold employee names
var empList = new List<string>();
//Loops through array to add names into list
foreach (string empl in empName)
{
empList.Add(empl.Trim());
}
ViewBag.selectEmployee = new SelectList(empList);
var model = new AppViewModel();
return View(new AppViewModel());
}
第二次更新:这是我对控制器的看法。
@model App_Catalog.Models.AppViewModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>AppViewModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.AppName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.AppName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AppName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Owner, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("selectEmployee", "Please Select an Employee")
</div>
</div>
【问题讨论】:
-
难道你误解了
First()的用法?另外,selectedEmployee = Request.Form["selectEmployee"].ToString();这行的目的是什么? -
您确定所选的员工姓名与数据库中的姓名完全相同吗?如果直接对数据库运行 SQL 查询,是否会得到类似的结果?
-
你为什么不使用 Where 而不是 Contains ?即在 selectedEmployee 中还有什么,你从下拉列表中得到了什么?
-
多久刷新一次? "var emp = Database.Session.Query
().AsEnumerable();" -
@Casper:使用同一行代码在 Http Get 方法中填充该列表,以从数据库中获取所有员工。所以我想每次用户访问页面时它都会刷新
标签: c# asp.net-mvc linq contains