我是 MVC 的新手,我的项目有一些问题。我想做的是一个用户可编辑的汽车租赁平台。在我的项目中,我希望有如下选项:
- 将汽车添加到数据库
- 使用数据库中所有汽车的下拉列表添加新的汽车租赁请求
- 注册/登录
- 查看数据库中每辆车的租金请求历史记录
项目将基于带有实体框架的 *.sdf 数据库。我在 Models/ 目录中创建了一个类文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace MVC4.Models
{
public class Car
{
public int ID {get; set;}
public string Make {get; set;}
public string Model {get; set;}
public virtual ICollection<Car> Cars{ get; set; }
}
public class Request
{
public int ID {get; set;}
public string User {get; set;}
public int CarID{ get; set; }
public virtual Car Car { get; set; }
}
public class CarDBContext : DbContext
{
public DbSet<Car> Cars { get; set; }
public DbSet<Request> Requests { get; set; }
}
}
控制器:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC4.Models;
namespace MVC4.Controllers
{
public class RequestController : Controller
{
private CarDBContext db = new Car1DBContext();
//
// GET: /Request/
public ViewResult Index()
{
return View(db.Requests.ToList());
}
//
// GET: /Request/Details/5
public ViewResult Details(int id)
{
Wypozyczenie1 Request = db.Requests.Find(id);
return View(request);
}
//
// GET: /Request/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Request/Create
[HttpPost]
public ActionResult Create(Request request)
{
if (ModelState.IsValid)
{
db.Requests.Add(request);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(request);
}
//
// GET: /Request/Edit/5
public ActionResult Edit(int id)
{
Request request= db.Requests.Find(id);
return View(request);
}
//
// POST: /Request/Edit/5
[HttpPost]
public ActionResult Edit(Request requests)
{
if (ModelState.IsValid)
{
db.Entry(request).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(request);
}
//
// GET: /Request/Delete/5
public ActionResult Delete(int id)
{
Request request= db.Requests.Find(id);
return View(request);
}
//
// POST: /Request/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Request request = db.Requests.Find(id);
db.Requests.Remove(request);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
创建请求视图:
........
<div class="editor-field">
ViewBag.PossibleCategories = context.Cars;
@Html.DropDownListFor(model => model.ID,
((IEnumerable<MVC4.Models.Car>)ViewBag.PossibleCategories)
.Select(option => new SelectListItem {
Text = (option == null ? "None" : option.Make),
Value = option.ID.ToString(),
Selected = (Model != null) && (option.ID == Model.CarID)
}), "Choose...")
@Html.ValidationMessageFor(model => model.CarID)
</div>
..........
这对我不起作用。如何填充此下拉列表并使此视图能够将数据插入数据库?谢谢你的帮助。我已经花了很多时间在这上面,但无法弄清楚。对不起我的英语,我希望你们能理解我。
我得到错误:
Value cannot be null.
Parameter name: source.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: source
Source Error:
Line 32: <div class="editor-field">
Line 33: ViewBag.PossibleCategories = context.Cars;
Line 34: @Html.DropDownListFor(model => model.ID,
Line 35: ((IEnumerable<MVC6.Models.Car>)ViewBag.PossibleCategories)
Line 36: .Select(option => new SelectListItem {
这是我的请求/视图:
@model IEnumerable<MVC6.Models.Request>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
User
</th>
<th>
Car
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.User)
</td>
<td>
@Html.DisplayFor(modelItem => item.Car.Make)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
</table>