性别的MVC4下拉菜单 [英] MVC4 Dropdown menu for Gender

查看:179
本文介绍了性别的MVC4下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MVC 4的新手,我想创建一个下拉菜单列出的性别。我尝试了很多,但没有什么帮助我,也是我的谷歌,但invain.Please帮助我创建下拉菜单性别请指导我。

解决方案

说我们使用枚举的性别:

 命名空间DropdownExample.Models 
{
public enum GenderType
{
男= 1,
女= 2
}
}

我们制作了一个这样的模型:

 使用System.Collections.Generic; 
使用System.ComponentModel.DataAnnotations;
使用System.Web.Mvc;
命名空间DropdownExample.Models
{
public class ActionModel
{
public ActionModel()
{
ActionsList = new List< SelectListItem> );
}
[Display(Name =Gender)]
public int ActionId {get;组; }
public IEnumerable< SelectListItem> ActionsList {get;组;
}
}

制作如下控制器:

  using System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Web.Mvc;
使用DropdownExample.Models;
命名空间DropdownExample.Controllers
{
public class ActionController:Controller
{
public ActionResult Index()
{
ActionModel model = new ActionModel ();
IEnumerable< GenderType> GenderType = Enum.GetValues(typeof(GenderType))
.Cast< GenderType>();
model.ActionsList = from action in actionTypes
选择新的SelectListItem
{
Text = action.ToString(),
Value =((int)action).ToString ()
};
return View(model);
}
}
}

那么在你看来,你使用DropDownListFor html帮助器,包括以下内容:

  @model DropdownExample.Models.ActionModel 
@ {
ViewBag.Title =索引;
}
@ Html.LabelFor(model => model.ActionId)
@ Html.DropDownListFor(model => model.ActionId,Model.ActionsList)

DropDownListFor html帮助器在这两个参数中使用:




  • 将保存所选值的属性的名称

  • 包含所有内容的列表< SelectListItem>()下拉列表中的选项。


I am new to MVC 4, i want to create a drop downmenu for listing the gender.I tried a lot but nothing helped me, also i google it but invain.Please help me in it tha thow to create dropdown menu for gender please guide me.

解决方案

say we use an enum for the gender:

namespace DropdownExample.Models
{
    public enum GenderType
    {
     Male=1,
     Female=2
    }
}

and we make a model like this:

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace DropdownExample.Models
{
    public class ActionModel
    {
        public ActionModel()
        {
            ActionsList = new List<SelectListItem>();
        }
        [Display(Name="Gender")]
        public int ActionId { get; set; }
        public IEnumerable<SelectListItem> ActionsList { get; set; }       
    }
}

make a controller like so:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using DropdownExample.Models;
namespace DropdownExample.Controllers
{
    public class ActionController : Controller
    {
        public ActionResult Index()
        {
            ActionModel model = new ActionModel();
            IEnumerable<GenderType> GenderType = Enum.GetValues(typeof(GenderType))
                                                       .Cast<GenderType>();
            model.ActionsList = from action in actionTypes
                                select new SelectListItem
                                {
                                    Text = action.ToString(),
                                    Value = ((int)action).ToString()
                                };
            return View(model);
        }
    }
}

then in your view, you use the DropDownListFor html helper you include the following:

@model DropdownExample.Models.ActionModel
@{
    ViewBag.Title = "Index";
} 
@Html.LabelFor(model=>model.ActionId)
@Html.DropDownListFor(model=>model.ActionId, Model.ActionsList)

the DropDownListFor html helper uses at leats these two parameters:

  • the name of the property that will hold the selected value
  • the List<SelectListItem>() that contains all the options in the dropdownlist.

这篇关于性别的MVC4下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆