我是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>
..........
这对我不起作用。 如何填充此下拉列表并使此View能够将数据插入数据库? 谢谢你的帮助。我花了很多时间在这上面,无法弄明白。对不起我的英文,我希望你们能理解我。
我收到错误:
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>
答案 0 :(得分:0)
我觉得让下拉列表也有点艰难,但最后我终于明白了......
像往常一样,有几种方法可以做到这一点。我是这样做的:
在控制器中,我创建了选择列表并放入了ViewBag
ViewBag.mylist = new SelectList(db.Whatever, "ValueColumn", "DisplayColumn");
然后在我的视图中:
Html.DropDownList("myname", (SelectList)ViewBag.mylist, "Select Something",
new
{
@onchange = "form.submit();"
})
如果您在表单上放置表单,那么这也会发布表单(如果您需要)。我认为你也可以在ViewModel中做这种事情。这对我来说似乎是最简单的方式,虽然看起来有点笨拙。很高兴听到反馈。
答案 1 :(得分:0)