C# MVC变量下拉菜单
我正在处理一个MVC4项目,我希望添加一个“搜索方式”下拉菜单,以便用户可以选择不同的变量进行搜索 我的模型C# MVC变量下拉菜单,c#,asp.net-mvc-4,lambda,C#,Asp.net Mvc 4,Lambda,我正在处理一个MVC4项目,我希望添加一个“搜索方式”下拉菜单,以便用户可以选择不同的变量进行搜索 我的模型 public class BloodStoredModel { [Required] public int ID { get; set; } [Required] [DisplayName("Blood Type")] public string bloodType { get; set; } [Required] [Displa
public class BloodStoredModel
{
[Required]
public int ID { get; set; }
[Required]
[DisplayName("Blood Type")]
public string bloodType { get; set; }
[Required]
[DisplayName("RH Factor")]
public string rhFactor { get; set; }
[Required]
[DisplayName("Last Name")]
public string donorLastName { get; set; }
[Required]
[DisplayName("First Name")]
public string donorFirstName { get; set; }
[Required]
[DisplayName("Address")]
public string address { get; set; }
[Required]
[DisplayName("Phone #")]
public string telephoneNumber { get; set; }
public class BloodDBContext : DbContext
{
public DbSet<BloodStoredModel> BloodStored { get; set; }
}
}
注释后的lambda表达式可以工作,但仅适用于该特定变量。我想做的是通过下拉列表动态选择
以下是我的相关观点
@using (Html.BeginForm()){
<p> @Html.DropDownList("list", ViewData["list"] as SelectList)
@Html.TextBox("SearchString")<br />
<input type="submit" value="Filter" /></p>
}
@使用(Html.BeginForm()){
@Html.DropDownList(“列表”,ViewData[“列表”]作为SelectList)
@Html.TextBox(“搜索字符串”)
}
该值在后端没有更改的原因是您使用的是DropDownList
而不是DropDownListFor
<一旦按下提交按钮(执行POST),code>DropDownList for将自动将选择绑定到您的模型。您必须在ViewModel
中包含下拉列表,或者使用JavaScript/Jquery调整前端
编辑
您必须创建一个新类:
public DropDownOption
{
public int Id {get; set;}
public string Description {get; set;}
}
创建视图模型
:
public myViewModel
{
public List<DropDownOption> DropDownOptions {get; set;}
public int OptionSelected {get; set;}
}
要使用用户选择的属性搜索BlookSearchModel的数组,需要使用反射。添加以下函数
private string GetValue(BloodStoredModel obj, string propertyName)
{
Type type = typeof(BloodStoredModel);
if (type != null)
{
PropertyInfo property = type.GetProperty(propertyName);
if (property != null)
{
return (string)property.GetValue(obj);
}
}
return null;
}
你可以这样做
bloodSearch = models.Where(s => string.Compare(GetValue(s, propertyName), searchString) == 0);
当然,对属性名称进行硬编码可能会导致错误。您可以使用反射来枚举属性名称
ViewBag.SearchFields = from prop in properties
select new SelectListItem() { Text = prop.Name };
这还将从模型中选择Id字段。但是,您可以修改linq语句以排除它
编辑我添加了以下内容。另外,我将上述代码更改为使用ViewBag而不是ViewData
要让您的视图使用它,您可以这样做
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "form1" }))
{
@Html.DropDownList("SearchFields")
<input type="submit" value="submit" onclick="return SetAction();" />
}
<script>
function SetAction() {
form = document.getElementById("form1");
dropdown = document.getElementById("SearchFields");
form.action = "/" + "?searchString=" + dropdown.value;
return true;
}
</script>
@使用(Html.BeginForm(null,null,FormMethod.Post,new{id=“form1”}))
{
@Html.DropDownList(“搜索字段”)
}
函数SetAction(){
form=document.getElementById(“form1”);
dropdown=document.getElementById(“搜索字段”);
form.action=“/”+”?searchString=“+dropdown.value;
返回true;
}
Ok,忘记了我在控制器中创建的整个列表,我该怎么做呢?我会想象@Html.DropDownListFor(BloodBankMVC.Models.BloodStoredModel)应该可以填充下拉列表,但它似乎不起作用。谢谢。对不起,我对MVC很陌生。MVC是否普遍接受viewmodels?是的,viewmodels被认为是MVC的最佳实践。您不应该在视图中处理实体模型。GetValue方法有错误。返回(字符串)属性.GetValue(obj);说它不包含一个参数的重载。哦,我用的是.NET4.5。它有一个接受一个参数的重载。您必须使用旧版本,但该版本不可用。请尝试为第二个参数传递null。是的,我使用的是.NET 4.0。很抱歉,我应该在前面声明。
bloodSearch = models.Where(s => string.Compare(GetValue(s, propertyName), searchString) == 0);
ViewBag.SearchFields = from prop in properties
select new SelectListItem() { Text = prop.Name };
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "form1" }))
{
@Html.DropDownList("SearchFields")
<input type="submit" value="submit" onclick="return SetAction();" />
}
<script>
function SetAction() {
form = document.getElementById("form1");
dropdown = document.getElementById("SearchFields");
form.action = "/" + "?searchString=" + dropdown.value;
return true;
}
</script>