C# 如何使用模型选择选项?
我对此一无所知 我有一个模型:C# 如何使用模型选择选项?,c#,javascript,jquery,asp.net-mvc,C#,Javascript,Jquery,Asp.net Mvc,我对此一无所知 我有一个模型: public class Filter { public string No { get; set; } public DateTime EndTime { get; set; } public string FilterStatus { get; set; } } 在我看来,我还有第二件事: <select id="status" name="status" style="width: 120px;font-size: 0.9
public class Filter
{
public string No { get; set; }
public DateTime EndTime { get; set; }
public string FilterStatus { get; set; }
}
在我看来,我还有第二件事:
<select id="status" name="status" style="width: 120px;font-size: 0.9em; padding: 0;" class="k-dropdown">
<option>-- Select --</option>
<option>Yes</option>
<option>No</option>
</select>
我要做的是选择选项,它与Model.FilterStatus相同。例如,如果状态为“是”,则下拉列表中的文本应为“是”。但有没有可能的办法呢?是否使用javascript?您可以使用Html.DropDown助手。此帮助程序将获得一个带有所显示选项的IEnumerable。
@model Filter
@Html.DropDownListFor(model => model.FilterStatus, new SelectList(
new List<SelectListItem> {
new SelectListItem { Text = "Yes", Value = "Yes" },
new SelectListItem { Text = "No", Value = "No" }
}, "Value", "Text"),
"--- Select Option ---", new { @class = "k-dropdown" })
在控制器中,您可以在ViewBag或使用视图模型中发送下拉列表的值。视图模型的此属性必须是一个包含SelectListItems的列表,其中包含两个选项Yes和No,然后使用过滤器的值选择正确的选项,如下所示:
ViewBag.Status = new List<SelectListItem>(){
new SelectListItem
{
Value = "Yes",
Text = "Yes",
Selected = yourFilter.FilterStatus //If your status is Yes this will be true
//otherwise false.
},
new SelectListItem
{
Value = "No",
Text = "No",
Selected = yourFilter.FilterStatus //If your status is No this will be true
//otherwise false.
}
};
//Don't create this class on the controller! This is only as an example.
public class FilterViewModel {
public bool Status {get; set;}
public IEnumerable<SelectListItem> statusDropDown {get; set;}
}
var myViewModel = new FilterViewModel();
myViewModel.statusDropDown = new List<SelectListItem>(){
new SelectListItem
{
Value = "Yes",
Text = "Yes",
Selected = yourFilter.FilterStatus
},
new SelectListItem
{
Value = "No",
Text = "No",
Selected = yourFilter.FilterStatus
}
};
return View(myViewModel);
使用ViewBag时,请注意属性的名称必须与帮助器上的名称匹配,并且该名称将与提交表单时表单将发送回控制器的名称相同
如果您使用的是视图模型版本,我建议您出于测试目的,您必须创建该视图模型并将下拉值分配给该属性,如下所示:
ViewBag.Status = new List<SelectListItem>(){
new SelectListItem
{
Value = "Yes",
Text = "Yes",
Selected = yourFilter.FilterStatus //If your status is Yes this will be true
//otherwise false.
},
new SelectListItem
{
Value = "No",
Text = "No",
Selected = yourFilter.FilterStatus //If your status is No this will be true
//otherwise false.
}
};
//Don't create this class on the controller! This is only as an example.
public class FilterViewModel {
public bool Status {get; set;}
public IEnumerable<SelectListItem> statusDropDown {get; set;}
}
var myViewModel = new FilterViewModel();
myViewModel.statusDropDown = new List<SelectListItem>(){
new SelectListItem
{
Value = "Yes",
Text = "Yes",
Selected = yourFilter.FilterStatus
},
new SelectListItem
{
Value = "No",
Text = "No",
Selected = yourFilter.FilterStatus
}
};
return View(myViewModel);
为什么不使用@Html.DropdownForso正确绑定?我还在控制器中使用FormCollection。我想我不能从下拉列表中发送数据。我也是mvc的新手。编辑您的问题并输入完整的代码,这样我们就可以了解情况,即控制器类和动作方法代码mvc部分,因为IMO FormCollection仅在其http POST和动作方法中使用感谢您的快速回答,但我还有一个补充。也许听起来很愚蠢,但是,它将如何工作?我的意思是,现在从我的下拉列表中选择的用于FormCollection。正如我所看到的,这个版本不能像那样使用。当然可以,但是为什么要使用FormCollection而不是您的模型,这样它就已经绑定了,例如[HttpPost]公共操作结果YourActionFilter模型{…它可以?如何?例如,现在我使用collection[status]来获取选中的选项,但是需要id,或者我可以做一些类似于new的事情{@class=k-dropdown,@Id=status}?HttpPost…这就是原因。现在将尝试它。检查示例添加了一些更改,但此示例几乎可以正常工作。谢谢。