现在,我正在处理 MVC 3 中的多选列表。今天早些时候,我克服了一个很大的障碍,即使用数据库中的版本数据填充下拉列表。问题是它显示了表格列 VERSION 中的每个项目。我知道这是一个简单的修复,但我似乎无法弄清楚.... 我在想我所要做的就是添加一个带有枚举器的 if 语句,该语句类似于以下伪代码。
while VERSION <> null
if version = version
then don't display version
end while
目前它正在显示所有 387 行版本,我需要它显示的是版本的第一个实例,所以如果版本是 1.5,它只显示第一个,所以我可以抓住它作为另一条记录,我希望这能让感觉。我在下面包含了我的控制器类和我的编辑类。如果您需要我的任何其他课程进行诊断,请告诉我。感谢您的帮助!
编辑 04/11/12
看来我需要澄清一些我的要求,所以这是我为你们做的尝试。
我需要帮助的是修复 selectList 代码,因此它只返回 VERSION 的第一个实例。当我单击下拉列表时,它会填充 VERSION 列中的每一行,这意味着我在下拉列表中有 385 个实例,表示版本 1.2 和两个 1.3 实例。我想做的是用版本 1.2 和 1.3 的两个实例填充下拉列表。请帮助如果我有更多积分,我会提供赏金,但我是新手,所以我只能说,如果你能提供帮助,我保证会投票!谢谢你的帮助!
控制器
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);
}
}
}
编辑.CSHTML
@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>