现在我正在研究MVC 3中的多选列表。我今天早些时候遇到了一个很大的障碍,即使用数据库中的版本数据填充下拉列表。问题是它显示表列VERSION中的每个项目。我知道这是一个简单的修复,但我似乎无法弄清楚....我在想我所要做的就是添加一个带有枚举器的if语句,该枚举器类似于下面的psudocode。
while VERSION <> null
if version = version
then don't display version
end while
目前它正在显示所有387行的VERSION,我需要它显示的是版本的第一个实例,所以如果版本是1.5它只显示第一个,所以我可以抓住它用于另一个记录,I希望这是有道理的。我在下面包含了我的控制器类和我的编辑类。如果您需要我的其他课程进行诊断,请告诉我。谢谢你的帮助!
似乎我需要澄清我的要求,所以这是我为你们做的尝试。
我需要帮助的是修复selectList代码,使它只返回VERSION的第一个实例。在我点击下拉列表的那一刻,它充满了VERSION列中的每一行,这意味着我在下拉列表中有385个实例,说版本1.2和两个1.3版本的实例。我希望它做的是只用版本1.2和1.3的两个实例来填充下拉列表。请帮助我会提供赏金,如果我有更多的积分,但我是新的,所以我只能说,如果你有所帮助我保证upvote!谢谢你的帮助!
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>
答案 0 :(得分:1)
SELECT DISTINCT
Version
FROM YourTable
这将为您提供一个独特的版本列表。目前尚不清楚您对控制器和视图的帮助。
编辑:
或者如果你想在你的控制器中使用linq(假设Version是db.iamp_mapping的属性)
db.iamp_mapping.Select(x => new iamp_mapping{Version = x.Version}).Distinct().ToList();