在mvc 4剃须刀中填充下拉列表 [英] populating dropdown in mvc 4 razor

查看:64
本文介绍了在mvc 4剃须刀中填充下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在mvc上的第一个应用程序并没有太多想法mvc 4 Razor

i想简单地从数据库中的一个页面插入数据,包括2个下拉列表首先用于城市,第二个用于状态我有三个表首先要插入哪些数据,其余两个用于城市和状态我想要做的是从城市和状态表绑定下拉列表并将其ID保存在主表中我为其创建了模型

This is my first application on mvc and haven't much idea of mvc 4 Razor
i want to simply insert data from one page in database including 2 dropdowns First for city and second for status i have three tables first on which data is to be inserted and rest two for city and status what i am trying to do is bind dropdown from city and status table and save its id in main table i have created model for it

 public class UserCallDetailModel
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }
        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 2)]
        [DataType(DataType.Text)]
        [Display(Name = "Name")]
        public string Name { get; set; }
        [Required]
        [StringLength(500, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 5)]
        [DataType(DataType.MultilineText)]
        [Display(Name = "Address")]
        public string Address { get; set; }
        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 10)]
        [DataType(DataType.PhoneNumber)]
        public string ContactNo { get; set; }
        [DataType(DataType.Text)]
        public string EmailID { get; set; }
        [DataType(DataType.Text)]
        public string ProductCat { get; set; }
        [DataType(DataType.Text)]
        public string OrgName { get; set; }
        [DataType(DataType.Text)]
        public string WebSite { get; set; }
        [DataType(DataType.Text)]
        public string FollowUpDetail { get; set; }
        [DataType(DataType.Date)]
        public DateTime FollowUpDate { get; set; }
        [DataType(DataType.Url)]
        public string VisitedSite { get; set; }

        public IEnumerable<SelectListItem> Employees { get; set; }
        [DataType(DataType.Currency)]
        public decimal FinalAmount { get; set; }
        [DataType(DataType.Date)]
        public DateTime LastDate { get; set; }

        public decimal CreatedBy { get; set; }

        public DateTime CreatedDate { get; set; }
        public decimal ModifyBy { get; set; }
        public DateTime ModifyDate { get; set; }
        public bool Enable { get; set; }
        public virtual ICollection<CustomerStatus> StatusID { get; set; }
        public virtual ICollection<City> CityID { get; set; }
    }

    public class CustomerStatus
    {
        public decimal StatusID { get; set; }
        public string StatusName { get; set; }
    }
    public class City
    {
        public decimal CityID { get; set; }
        public string CityName { get; set; }
    }

and controller like using (var db = new TenderKhabarCRMModels())
             {
                SelectList selectList = new SelectList(db.City, "CityID", "CityName");
                ViewBag.EmployeeList = selectList;

                 db.UserCallDetailModel.Add(model);
                 db.SaveChanges();
             }

             return RedirectToAction("UserCallDetailList");
but nothing is happening can some one help 

推荐答案

创建一个类下面的下拉列表



试试这样

Create a class Dropdownlist like below

Try like this
using System.Collections.Generic;using System.Web.Mvc;

namespace EDaaS.Domain.Utilities
{
    public static class DropDownList<T>
    {
        public static SelectList LoadItems(IList<T> collection, string value, string text)
        {
            return new SelectList(collection, value, text);
        }
    }

}



您的观点


Your View

@Html.DropDownListFor(model => model.CityID, (IEnumerable<SelectListItem>)ViewBag.EmployeeList, "--Select--", new { @class = "select" })





你的ActionResult



Your ActionResult

ViewBag.EmployeeList =
                Domain.Utilities.DropDownList<City>.LoadItems(db.City.ToList(), "CityID", "CityName");



希望这有帮助


Hope this helps


请在页面渲染上填充列表如ViewBag.List = ......



@ Html.DropDownListFor(model => model.Id,new SelectList(Vie) wBag.List,value,text,Model.SelectedId))
Please on page render populate the list as ViewBag.List= ......

@Html.DropDownListFor(model => model.Id, new SelectList(ViewBag.List, "value", "text", Model.SelectedId))


这篇关于在mvc 4剃须刀中填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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