我获得了code,这有助于我通过下拉菜单为具有管理员状态的用户分配故障单。我无法跟踪控制器中的代码,不知道我是否正确执行了该操作。
代码我正在尝试转换TicketController
public ActionResult Index()
{
TicketVM model = new TicketVM();
ConfigureViewModel(model);
return View(model);
}
[HttpPost]
public ActionResult Index(TicketVM model)
{
if (!ModelState.IsValid)
{
ConfigureViewModel(model);
return View(model);
}
// Initialise a new data model
// Set the author based on the current user
// Set the other properties based on the view model e.g
Ticket ticket = new Ticket
{
// Author = xxx
Issue = model.Issue,
IssuedTo = model.IssuedTo
};
// Save the data model and redirect e.g.
//db.Users.Add(ticket);
//db.SaveChanges();
// Cant have more than one view in DotNetFiddle so just return the view
return View(model);
}
TicketController.cs(我的代码)
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RecreationalServicesTicketingSystem.Models;
using RecreationalServicesTicketingSystem.DAL;
using RecreationalServicesTicketingSystem.ViewModels;
namespace RecreationalServicesTicketingSystem.Controllers
{
public class TicketController : Controller
{
private IssueContext db = new IssueContext();
//
// GET: /Ticket/
public ActionResult Create()
{
TicketVM model = new TicketVM();
ConfigureViewModel(model);
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(TicketVM model)
{
if (!ModelState.IsValid)
{
ConfigureViewModel(model);
return View(model);
}
Ticket ticket = new Ticket {
Issue = model.Issue,
IssuedTo = model.IssuedTo
};
return View(model);
}
private void ConfigureViewModel(TicketVM model)
{
IEnumerable<User> admins = db.Users.Where(u => u.IsAdministrator).OrderBy(u => u.LastName);
model.AdministratorList = admins.Select(a => new SelectListItem
{
Value = a.UserID.ToString(),
Text = string.Format("{0} {1}", a.FirstMidName, a.LastName)
});
}
//
// GET: /Ticket/Details/5
public ActionResult Details(int id = 0)
{
Ticket ticket = db.Tickets.Find(id);
if (ticket == null)
{
return HttpNotFound();
}
return View(ticket);
}
// POST: /Ticket/Create
//
// GET: /Ticket/Edit/5
public ActionResult Edit(int id = 0)
{
Ticket ticket = db.Tickets.Find(id);
if (ticket == null)
{
return HttpNotFound();
}
ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", ticket.CategoryID);
ViewBag.UserID = new SelectList(db.Users, "UserID", "LastName", ticket.UserID);
return View(ticket);
}
//
// POST: /Ticket/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(TicketVM model)
{
if (!ModelState.IsValid)
{
ConfigureViewModel(model);
return View(model);
}
Ticket ticket = new Ticket
{
// Author = xxx
Issue = model.Issue,
IssuedTo = model.IssuedTo
};
ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", ticket.CategoryID);
ViewBag.UserID = new SelectList(db.Users, "UserID", "LastName", ticket.UserID);
return View(ticket);
}
//
// GET: /Ticket/Delete/5
public ActionResult Delete(int id = 0)
{
Ticket ticket = db.Tickets.Find(id);
if (ticket == null)
{
return HttpNotFound();
}
return View(ticket);
}
//
// POST: /Ticket/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Ticket ticket = db.Tickets.Find(id);
db.Tickets.Remove(ticket);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
Ticket.cs
public enum Priority
{
Low, Med, High
}
public class Ticket
{
public int? TicketID { get; set; }
public string Issue { get; set; }
[DisplayFormat(NullDisplayText = "No Priority")]
public Priority? Priority { get; set; }
[ForeignKey("CategoryID")]
public virtual Category Category { get; set; }
public int CategoryID { get; set; }
public int UserID { get; set; }
[ForeignKey("UserID")]
public virtual User User { get; set; }
public int Author { get; set; }
public int IssuedTo { get; set; }
}
\的ViewModels \ TicketVM.cs
public class TicketVM
{
public int? UserID { get; set; }
[Required(ErrorMessage = "Please enter the description")]
public string Issue { get; set; }
[Display(Name = "Administrator")]
[Required(ErrorMessage = "Please select the Administrator")]
public int IssuedTo { get; set; }
public IEnumerable<SelectListItem> AdministratorList { get; set; }
public int CategoryID { get; set; }
public IEnumerable<SelectListItem> CategoryList { get; set; }
}
\视图\票务\ Create.cshtml
@model RecreationalServicesTicketingSystem.ViewModels.TicketVM
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Ticket</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Issue)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Issue)
@Html.ValidationMessageFor(model => model.Issue)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CategoryID, "Category")
</div>
<div class="editor-field">
@Html.DropDownList("CategoryID", String.Empty)
@Html.ValidationMessageFor(model => model.CategoryID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UserID, "User")
</div>
<div class="editor-field">
@Html.DropDownList("UserID", String.Empty)
@Html.ValidationMessageFor(model => model.UserID)
</div>
<div class="editor-field">
@Html.DropDownListFor(m => m.UserID, (IEnumerable<SelectListItem>)ViewBag.AllUsers, "Please select")`
@Html.ValidationMessageFor(model => model.UserID)
</div>
@Html.HiddenFor(m => m.TicketID)
<div class="form-group">
@Html.LabelFor(m => m.Issue)
@Html.TextBoxFor(m => m.Issue, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Issue)
</div>
<div class="form-group">
@Html.LabelFor(m => m.IssuedTo)
@Html.DropDownListFor(m => m.IssuedTo, Model.AdministratorList, "Please select", new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.IssuedTo)
</div>
<button type="submit" class="btn btn-success submit">Save</button>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
答案 0 :(得分:2)
我给了你那段代码(来自this DotNetFiddle),但很清楚你还没有理解它。
删除您的Create()
方法,并将Index()
方法重命名为Create()
。
如果你想在Categories
的视图中添加一个SelectList,那么添加一个属性(比方说)
public int Category{ get; set; }
public IEnumerable<SelectListItem> CategoryList { get; set; }
到视图模型(使用视图模型时不要使用ViewBag
)并将用于填充集合的代码放在ConfigureViewModel()
方法中。
Edit
方法与Index()
方法中当前显示的代码类似,主要区别在于您是否根据ID获取现有数据模型,并将其属性映射到新方法在将视图模型传递给视图之前TicketVM
的实例。在POST方法中,您将根据ID再次获取数据模型,并将视图模型属性映射到该模型,然后保存数据模型。
答案 1 :(得分:1)
您正在尝试从视图模型中获取这些值,但您没有将该视图模型从控制器传递到视图。如果您使用DropDownListFor
,则需要先在控制器中初始化AdministratorList
属性,然后将其传递给视图View(model)
。如果您使用ViewBag选项,则无需传递视图模型,但需要在视图中使用DropDownList
,如下所示:
ViewBag.MyDropdownList = new SelectList(Items, "PropertyOfItemUsedForValue", "PropertyOfItemUsedForText");
然后在您的视图中添加您的下拉列表
@Html.DropDownList("MyDropdownList", null, htmlAttributes: new { @class = "form-control"})
答案 2 :(得分:0)