如何创建MVC4下拉列表 [英] How to create a dropDown list in MVC4
问题描述
您能帮我创造asp.net mvc的4下拉列表?
我有一个ProjetoFuncionario类是一个关联类Projeto和Funcionario
的我应该为下拉列表创建一个类?
我有在创造一个下拉列表,非常困难。
有一些很好的教程或小费你们可以给我吗?
谢谢!
CLASS PROJETOFUNCIONARIO
命名空间Exercicio1.Models
{
公共类ProjetoFuncionario
{
公共Projeto IdProjeto {搞定;组; }
公共Funcionario IdFuncionario {搞定;组; } }
}
解决方案
一个下拉列表用于从可能的选项列表中选择一个值
Projeto
和 Funcionario
,然后创建一个视图模型重新present要显示和编辑(什么是什么视图模型)。注意这个例子是基于从previous问题的模型定义。 公共类ProjetoFuncionarioVM
{
[显示(NAME =Projeto)]
众长SelectedProjeto {搞定;组; }
[显示(NAME =Funcionario)]
众长SelectedFuncionario {搞定;组; }
公众的SelectList ProjetoList {搞定;组; }
公众的SelectList FuncionarioList {搞定;组; }
}
控制器
公众的ActionResult编辑()
{
ProjetoFuncionarioVM模式=新ProjetoFuncionarioVM();
model.SelectedProjeto =如果你想preSELECT //的选项之一
ConfigureEditModel(模型);
返回查看(模型);
}公众的ActionResult编辑(ProjetoFuncionarioVM模型)
{
如果(ModelState.IsValid)
{
ConfigureEditModel(模型); //重新分配选择列表
返回查看(模型); //返回纠正错误的观点
}
//视图模型的访问属性,保存和重定向
}私人无效ConfigureEditModel(ProjetoFuncionarioVM模型)
{
清单< Projeto> projeto = //获取projeto对象从数据库中收集
清单< Funcionario> funcionario = //同上,用于funcionario对象
//指定选择列表
model.ProjetoList =新的SelectList(projetoid_Projeto,nome_Projeto);
model.FuncionarioList =新的SelectList(funcionarioid_funcionario,nom_funcionario);
}
查看
@model ProjetoFuncionarioVM@using(Html.BeginForm())
{
@ Html.LabelFor(M = GT; m.SelectedProjeto)
@ Html.DropDownListForFor(M = GT; m.SelectedProjeto,Model.ProjetoList,请选择)
@ Html.ValidationMessageFor(M = GT; m.SelectedProjeto)
@ Html.LabelFor(M = GT; m.SelectedFuncionario)
@ Html.DropDownListForFor(M = GT; m.SelectedFuncionario,Model.FuncionarioList,请选择)
@ Html.ValidationMessageFor(M = GT; m.SelectedFuncionario)
<输入类型=提交值=保存/>
}
另请参阅的SelectList 和<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.html.selectextensions.dropdownlistfor(v=vs.118).aspx\"相对=nofollow> DropDownListFor 对这些方法的各种重载
Could you help me create a dropdown list in asp.net mvc 4?
I Have a ProjetoFuncionario class that is an associative class of "Projeto" and "Funcionario"
I should create a class for the dropdown?
I am having very difficult in creating a dropdown list. Has some good tutorial or a tip you guys can give me?
Thanks!
CLASS PROJETOFUNCIONARIO
namespace Exercicio1.Models
{
public class ProjetoFuncionario
{
public Projeto IdProjeto { get; set; }
public Funcionario IdFuncionario { get; set; }
}
}
A drop down list is used for selecting a value from a list of possible options. Its not clear from you model, but assuming you want a view with drop downs to select a Projeto
and a Funcionario
, then create a view model to represent what you want to display and edit (What is a view model). Note this example is based on the model definitions from your previous question.
public class ProjetoFuncionarioVM
{
[Display(Name="Projeto")]
public long SelectedProjeto { get; set; }
[Display(Name="Funcionario")]
public long SelectedFuncionario { get; set; }
public SelectList ProjetoList { get; set; }
public SelectList FuncionarioList { get; set; }
}
Controller
public ActionResult Edit()
{
ProjetoFuncionarioVM model = new ProjetoFuncionarioVM();
model.SelectedProjeto = // if you want to preselect one of the options
ConfigureEditModel(model);
return View(model);
}
public ActionResult Edit(ProjetoFuncionarioVM model)
{
if (ModelState.IsValid)
{
ConfigureEditModel(model); // reassign the select lists
return View(model); // return the view to correct errors
}
// access properties of the view model, save and redirect
}
private void ConfigureEditModel(ProjetoFuncionarioVM model)
{
List<Projeto> projeto = // get the collection of projeto objects from the database
List<Funcionario> funcionario = // ditto for funcionario objects
// Assign select lists
model.ProjetoList = new SelectList(projeto, "id_Projeto", "nome_Projeto");
model.FuncionarioList = new SelectList(funcionario, "id_funcionario", "nom_funcionario");
}
View
@model ProjetoFuncionarioVM
@using (Html.BeginForm())
{
@Html.LabelFor(m => m.SelectedProjeto)
@Html.DropDownListForFor(m => m.SelectedProjeto, Model.ProjetoList, "Please select")
@Html.ValidationMessageFor(m => m.SelectedProjeto)
@Html.LabelFor(m => m.SelectedFuncionario)
@Html.DropDownListForFor(m => m.SelectedFuncionario, Model.FuncionarioList, "Please select")
@Html.ValidationMessageFor(m => m.SelectedFuncionario)
<input type="submit" value="Save" />
}
Refer also SelectList and DropDownListFor for various overloads of these methods
这篇关于如何创建MVC4下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!