Asp.net mvc 3 在MVC3中使用多选列表

Asp.net mvc 3 在MVC3中使用多选列表,asp.net-mvc-3,multi-select,Asp.net Mvc 3,Multi Select,现在我正在MVC3中处理一个多选列表。今天早些时候,我克服了一个很大的障碍,那就是用数据库中的版本数据填充下拉列表。问题是它显示了表列版本中的每个项。我知道这是一个简单的解决方案,但我似乎无法理解。。。。我在想,我所要做的就是添加一个if语句,其中包含一个枚举器,该枚举器表示如下psudocode while VERSION <> null if version = version then don't display version end while 而版本为空 如果版本

现在我正在MVC3中处理一个多选列表。今天早些时候,我克服了一个很大的障碍,那就是用数据库中的版本数据填充下拉列表。问题是它显示了表列版本中的每个项。我知道这是一个简单的解决方案,但我似乎无法理解。。。。我在想,我所要做的就是添加一个if语句,其中包含一个枚举器,该枚举器表示如下psudocode

while VERSION <> null 
if version = version 
then don't display version
end while 
而版本为空
如果版本=版本
然后不显示版本
结束时
目前,它正在显示所有387行的版本,我需要它显示的是一个版本的第一个实例,所以如果版本是1.5,它只显示第一个,这样我就可以抓取另一个记录,我希望这是有意义的。我在下面包含了我的控制器类和编辑类。如果您需要我的其他课程进行诊断,请告诉我。谢谢你的帮助

2012年11月4日编辑 看来我需要澄清我的一些要求,所以这里是我的尝试,为你们

我需要的帮助是修复selectList代码,使其只返回版本的第一个实例。当我点击下拉列表时,它会被列VERSION中的每一行填满,这意味着下拉列表中有385个实例,分别是VERSION 1.2和VERSION 1.3。我想让它做的是用版本1.2和1.3的两个实例来填充下拉列表。请帮助我,如果我有更多的分数,我会提供一个悬赏,但我是新的,所以我能说的是,如果你帮助我,我保证投票!谢谢你的帮助

paccontroller.CS
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Data.Linq;
使用System.Data.Linq.Mapping;
使用System.Web.UI.WebControl;
使用System.Web;
使用System.Web.Mvc;
使用DBFirstMVC.Models;
使用系统数据;
使用页面列表;
使用PagedList.Mvc;
使用DBFirstMVC.Controllers;
使用System.IO;
使用dbmvc;
使用System.Web.UI;
命名空间DBFirstMVC.Controllers
{
公共类paccontroller:Controller
{
PaEntities db=新PaEntities();
//指数法
公共视图结果索引(字符串排序器、字符串currentFilter、字符串searchString、int?页)
{
ViewBag.CurrentSort=sortOrder;//ViewBag属性为视图提供当前排序顺序
ViewBag.NameSortParm=String.IsNullOrEmpty(排序器)?“PA desc”:“;//调用排序器开关/case PA desc或默认值
ViewBag.MPSortParm=sortOrder==“MP”?“MP desc”:“MP asc”//调用排序器开关/case MP desc或MP asc
ViewBag.IASortParm=sortOrder==“IA”?“IA desc”:“IA asc”//调用排序器开关/case IA desc或IA asc
ViewBag.VersionSortParm=sortOrder==“VERSION”?“VERSION desc”:“VERSION asc”//调用sortOrder开关/case VERSION desc或VERSION asc
ViewBag.IAMP_PKSortParm=sortOrder=“IAMP_PK”?“IAMP_PK desc”:“IAMP_PK asc”;//调用排序器开关/case IAMP_PK desc或IAMP_PK asc
if(Request.HttpMethod==“GET”)
{
searchString=currentFilter;//将currentFilter设置为searchString
}
其他的
{
page=1;//默认为第1页
}
ViewBag.CurrentFilter=searchString;//为视图提供当前筛选器字符串
var IAMP=从数据库中的p开始。IAMP_映射选择p;
如果(!String.IsNullOrEmpty(searchString))
{
IAMP=IAMP.Where(p=>p.PA.ToUpper().Contains(searchString.ToUpper());//仅选择包含搜索字符串的记录
}
switch(sortOrder)//开关大小写根据所需的排序进行更改
{
案例“Pa desc”:
IAMP=IAMP.OrderByDescending(p=>p.PA);
打破
案例“MP desc”:
IAMP=IAMP.OrderByDescending(p=>p.MAJOR\u程序);
打破
案例“MP asc”:
IAMP=IAMP.OrderBy(p=>p.MAJOR\u程序);
打破
案例“IA desc”:
IAMP=IAMP.OrderByDescending(p=>p.INVESTMENT\U区域);
打破
案例“IA asc”:
IAMP=IAMP.OrderBy(p=>p.INVESTMENT\U区域);
打破
案例“asc版本”:
IAMP=IAMP.OrderBy(p=>p.VERSION);
打破
案例“版本描述”:
IAMP=IAMP.OrderByDescending(p=>p.VERSION);
打破
案例“IAMP_PK asc”:
IAMP=IAMP.OrderBy(p=>p.IAMP\u PK);
打破
案例“IAMP_PK desc”:
IAMP=IAMP.OrderByDescending(p=>p.IAMP_PK);
打破
违约:
IAMP=IAMP.OrderBy(p=>p.PA);
打破
}
int pageSize=15;//显示的记录数
int pageNumber=(第1页);//起始页码
返回视图(IAMP.ToPagedList(pageNumber,pageSize));//使用pagedList方法返回正确的页面值
}
//实例化创建方法
//获取:/Pa/创建
公共操作结果创建()
{
SetVersionViewBag();
返回视图();
}
//Create方法将记录添加到数据库并保存更改
//POST:/Pa/创建
[HttpPost]
公共行动结果创建(iamp_映射iamp)
{
尝试
{
使用(var db=new paenties())
{
db.iamp_mapping.Add(iamp);
db.SaveChanges();
}
返回重拨
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Web.UI.WebControls;
using System.Web;
using System.Web.Mvc;
using DBFirstMVC.Models;
using System.Data;
using PagedList;
using PagedList.Mvc;
using DBFirstMVC.Controllers;
using System.IO;
using DBFirstMVC;
using System.Web.UI;





namespace DBFirstMVC.Controllers

{
    public class PaController : Controller
    {
        PaEntities db = new PaEntities();

        // Index Method 
        public ViewResult Index(string sortOrder, string currentFilter, string     searchString, int? page)
        {
            ViewBag.CurrentSort = sortOrder; //ViewBag property provides the view with the current sort order
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "PA desc" : ""; // Calls the sortOrder switch/case PA desc or default 
            ViewBag.MPSortParm = sortOrder == "MP" ? "MP desc" : "MP asc"; // Calls the sortOrder switch/case MP desc or MP asc
            ViewBag.IASortParm = sortOrder == "IA" ? "IA desc" : "IA asc"; // Calls the sortOrder switch/case IA desc or IA asc
            ViewBag.VersionSortParm = sortOrder == "VERSION" ? "Version desc" : "Version asc"; // Calls the sortOrder switch/case Version desc or Version asc
            ViewBag.IAMP_PKSortParm = sortOrder == "IAMP_PK" ? "IAMP_PK desc" : "IAMP_PK asc"; // Calls the sortOrder switch/case IAMP_PK desc or IAMP_PK asc

            if (Request.HttpMethod == "GET") 
            {
                searchString = currentFilter; //sets the currentFilter equal to Searchstring
            }
            else
            {
                page = 1;                   // defaults to page 1
            }
            ViewBag.CurrentFilter = searchString; // Provides the view with the current filter string


            var IAMP = from p in db.iamp_mapping select p;

            if (!String.IsNullOrEmpty(searchString))
            {
                IAMP = IAMP.Where(p => p.PA.ToUpper().Contains(searchString.ToUpper())); //selects only records that contains the search string
            }

            switch (sortOrder) // switch case changes based on desired sort 
            {
                case "Pa desc":
                    IAMP = IAMP.OrderByDescending(p => p.PA);
                    break;
                case "MP desc":
                    IAMP = IAMP.OrderByDescending(p =>p.MAJOR_PROGRAM);
                    break;
                case "MP asc":
                    IAMP = IAMP.OrderBy(p =>p.MAJOR_PROGRAM);
                    break;
                case "IA desc":
                    IAMP = IAMP.OrderByDescending(p => p.INVESTMENT_AREA);
                    break;
                case "IA asc":
                    IAMP = IAMP.OrderBy(p => p.INVESTMENT_AREA);
                    break;
                case "Version asc":
                    IAMP = IAMP.OrderBy(p => p.VERSION);
                    break;
                case "Version desc":
                    IAMP = IAMP.OrderByDescending(p => p.VERSION);
                    break;
                case "IAMP_PK asc":
                    IAMP = IAMP.OrderBy(p => p.IAMP_PK);
                    break;
                case "IAMP_PK desc":
                    IAMP = IAMP.OrderByDescending(p => p.IAMP_PK);
                    break;
                default:
                    IAMP = IAMP.OrderBy(p => p.PA);
                    break;
            }
            int pageSize = 15; // number of records shown
            int pageNumber = (page ?? 1); // start page number

            return View(IAMP.ToPagedList(pageNumber, pageSize)); // uses pagedList method to return correct page values
        }


        // Instantiates create method
        // GET: /Pa/Create

        public ActionResult Create()
        {
            SetVersionViewBag();
            return View();
        }

        // Create method adds records to Database and saves changes 
        // POST: /Pa/Create

        [HttpPost]
        public ActionResult Create(iamp_mapping IAMP)
        {
            try
            {
                using (var db = new PaEntities())
                {
                    db.iamp_mapping.Add(IAMP);
                    db.SaveChanges();
                }

                return RedirectToAction("Index");
            }
            catch
            {
                ViewBag.VERSION = new MultiSelectList(db.iamp_mapping, "VERSION", "VERSION", IAMP.VERSION);
                return View(IAMP);
            }
        }

        // Instantiates Edit Method
        // GET: /Pa/Edit/5

        public ActionResult Edit(string id)
        {

            using (var db = new PaEntities())
            {
                iamp_mapping IAMP = db.iamp_mapping.Find(id);
                SetVersionViewBag(IAMP.VERSION);
                return View(IAMP);
            }
        }

        // Edit method modifies existing records and saves changes
        // POST: /Pa/Edit/5

        [HttpPost]
        public ActionResult Edit(string id, iamp_mapping IAMP)
        {
            try
            {
                using (var db = new PaEntities())
                {
                    db.Entry(IAMP).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("");
                }
            }
            catch
            {
                SetVersionViewBag(IAMP.VERSION);
                return View(IAMP);
            }
        }

        // Instantiates delete method
        // GET: /Pa/Delete/5

        public ActionResult Delete(string id)
        {
            using (var db = new PaEntities())
            {

                return View(db.iamp_mapping.Find(id));
            }
        }

        // Delete method renames primary key and then removes record from database
        // POST: /Pa/Delete/5

        [HttpPost]
        public ActionResult Delete(string id, iamp_mapping IAMP)
        {
            try
            {
                using (var db = new PaEntities())
                {
                    var vIAMP = db.iamp_mapping.Find(id);
                    db.Entry(vIAMP).State = EntityState.Deleted;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }

            }
            catch (Exception e)
            {
                throw (e);
                //return View();
            }
        }
        public ActionResult IAMP_Mapping(iamp_mapping IAMP)
        {
            var iamp_mapping = db.iamp_mapping as IEnumerable<iamp_mapping>;
            var grid = new GridView
            {
                DataSource = from p in iamp_mapping
                             select new
                             {
                                 PA = p.PA,
                                 MP = p.MAJOR_PROGRAM,
                                 IA = p.INVESTMENT_AREA,
                                 VERSION = p.VERSION,
                                 IAMP_PK = p.IAMP_PK
                             }
            };
            grid.DataBind();

            Response.ClearContent();
            Response.AddHeader("content-dispostion", "inline; filename= Excel.xls");

            Response.ContentType = "application/vnd.ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            grid.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
            return View("Index");
        }

        public ActionResult SelectVersion() {
            List<SelectListItem> versions = new List<SelectListItem>();



            versions.Add(new SelectListItem { Text = "Action", Value = "0" });

            versions.Add(new SelectListItem { Text = "Drama", Value = "1" });

            versions.Add(new SelectListItem { Text = "Comedy", Value = "2", Selected = true });

            versions.Add(new SelectListItem { Text = "Science Fiction", Value = "3" });

            ViewBag.VersionType = versions;

            return View();


            }
        public ViewResult VersionChosen(string VersionType)
        {
            ViewBag.messageString = VersionType;
            return View("Information");
        }

        public enum eVersionCategories { Action, Drama, Comedy, Science_Fiction };

        private void SetViewBagVersionType(eVersionCategories selectedVersion)
        {

            IEnumerable<eVersionCategories> values =
                Enum.GetValues(typeof(eVersionCategories))

                .Cast<eVersionCategories>();

            IEnumerable<SelectListItem> versions =
                from value in values
                select new SelectListItem
                {
                    Text = value.ToString(),

                    Value = value.ToString(),

                    Selected = value == selectedVersion,
                };


            ViewBag.VersionType = versions;

        }

        public ActionResult SelectVersionEnum()
        {

            SetViewBagVersionType(eVersionCategories.Drama);

            return View("SelectVersion");

        }

        public ActionResult SelectVersionEnumPost()
        {

            SetViewBagVersionType(eVersionCategories.Comedy);

            return View();

        }

        [HttpPost]

        public ActionResult SelectVersionEnumPost(eVersionCategories VersionType)
        {

            ViewBag.messageString = VersionType.ToString() + " val = " + (int)VersionType;


            return View("Information");
        }

        private void SetVersionViewBag(string VERSION = null)
        {

            if (VERSION == null)
                ViewBag.VERSION = new MultiSelectList(db.iamp_mapping, "VERSION", "VERSION");
            else
                ViewBag.VERSION = new MultiSelectList(db.iamp_mapping.ToArray(), "VERSION", "VERSION", VERSION);

        }



















    }
}
@model DBFirstMVC.Models.iamp_mapping

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>



@using (Html.BeginForm()) {

    @Html.ValidationSummary(true)
    <fieldset>
        <legend>iamp_mapping</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.PA)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.PA)
            @Html.ValidationMessageFor(model => model.PA)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.MAJOR_PROGRAM)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.MAJOR_PROGRAM)
            @Html.ValidationMessageFor(model => model.MAJOR_PROGRAM)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.INVESTMENT_AREA)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.INVESTMENT_AREA)
            @Html.ValidationMessageFor(model => model.INVESTMENT_AREA)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.VERSION)
        </div>
        <div class="editor-field">

            @Html.DropDownList("Version", ViewBag.Version as MultiSelectList)
            @Html.ValidationMessageFor(model => model.VERSION)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.IAMP_PK)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.IAMP_PK)
            @Html.ValidationMessageFor(model => model.IAMP_PK)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "")
</div>
SELECT DISTINCT
     Version
FROM YourTable
db.iamp_mapping.Select(x => new iamp_mapping{Version = x.Version}).Distinct().ToList();