我在ASP.NET Model-View-Controller中的项目遇到问题。
我有一个应用程序“ Guestbook”。我可以成功创建来宾,但是不能对其进行编辑。
看:1.我启动应用程序-一切正常
2.我创建一个访客-一切正常(来宾具有所有输入的数据)
3.我单击“编辑”-一切正常
4.我看到新窗口。屏幕上的所有字段都具有正确的数据,但是我在编辑功能中有一个断点,并且我看到所有变量都为空。
模型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
namespace Guestbook.Models
{
public class Guest
{
public int ID { get; set; }
[Display(Name = "name", ResourceType=typeof(Resources.Resources))]
[Required(ErrorMessageResourceType = typeof(Resources.Resources),
ErrorMessageResourceName = "nameRequired")]
[StringLength(50, ErrorMessageResourceType = typeof(Resources.Resources),
ErrorMessageResourceName = "nameLong")]
public string name { get; set; }
[Display(Name = "surname", ResourceType = typeof(Resources.Resources))]
[Required(ErrorMessageResourceType = typeof(Resources.Resources),
ErrorMessageResourceName = "surnameRequired")]
[StringLength(50, ErrorMessageResourceType = typeof(Resources.Resources),
ErrorMessageResourceName = "surnameLong")]
public string surname { get; set; }
[Display(Name = "firm", ResourceType = typeof(Resources.Resources))]
[Required(ErrorMessageResourceType = typeof(Resources.Resources),
ErrorMessageResourceName = "firmRequired")]
[StringLength(50, ErrorMessageResourceType = typeof(Resources.Resources),
ErrorMessageResourceName = "firmLong")]
public string firm { get; set; }
[Display(Name = "toWhom", ResourceType = typeof(Resources.Resources))]
[Required(ErrorMessageResourceType = typeof(Resources.Resources),
ErrorMessageResourceName = "toWhomRequired")]
[StringLength(50, ErrorMessageResourceType = typeof(Resources.Resources),
ErrorMessageResourceName = "toWhomLong")]
public string toWhom { get; set; }
[Display(Name = "permitNumber", ResourceType = typeof(Resources.Resources))]
[Required(ErrorMessageResourceType = typeof(Resources.Resources),
ErrorMessageResourceName = "permitRequired")]
[Range(0, 10000, ErrorMessageResourceType = typeof(Resources.Resources),
ErrorMessageResourceName = "permitRange")]
public int permitNumber { get; set; }
[Display(Name = "magazine", ResourceType = typeof(Resources.Resources))]
[Required(ErrorMessageResourceType = typeof(Resources.Resources),
ErrorMessageResourceName = "magazineRequired")]
public bool magazine { get; set; }
[Display(Name = "entranceTime", ResourceType = typeof(Resources.Resources))]
[DataType(DataType.Date)]
public DateTime? entranceTime { get; set; }
[Display(Name = "entranceTimeTime", ResourceType = typeof(Resources.Resources))]
public string entranceTimeTime { get; set; }
[Display(Name = "exitDate", ResourceType = typeof(Resources.Resources))]
[DataType(DataType.Date)]
public DateTime? exitDate { get; set; }
[Display(Name = "exitTime", ResourceType = typeof(Resources.Resources))]
public string exitTime { get; set; }
}
public class GuestDBContext : DbContext
{
public DbSet<Guest> Guests { get; set; }
}
}
控制器:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Guestbook.Models;
namespace Guestbook.Controllers
{
public class GuestsController : BaseController
{
private GuestDBContext db = new GuestDBContext();
// GET: Guests
public ActionResult Index()
{
//Sortowanie po nazwisku.
var guests = from s in db.Guests select s;
guests = guests.OrderBy(s => s.surname);
return View(guests.ToList());
}
// GET: Guests/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Guest guest = db.Guests.Find(id);
if (guest == null)
{
return HttpNotFound();
}
return View(guest);
}
// GET: Guests/Create
public ActionResult Create()
{
return View();
}
// POST: Guests/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,name,surname,firm,toWhom,permitNumber,magazine,entranceTime,entranceTimeTime,exitDate,exitTime")] Guest guest)
{
guest.entranceTime = DateTime.Today;
guest.exitDate = DateTime.Today;
DateTime dt = DateTime.Now;
string format = "HH:mm:ss";
guest.entranceTimeTime = dt.ToString(format);
guest.exitTime = dt.ToString(format);
if (ModelState.IsValid)
{
db.Guests.Add(guest);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(guest);
}
// GET: Guests/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Guest guest = db.Guests.Find(id);
if (guest == null)
{
return HttpNotFound();
}
return View(guest);
}
// POST: Guests/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "exitDate,exitTime")] Guest g)
{
g.exitDate = DateTime.Today;
DateTime dt = DateTime.Now;
string format = "HH:mm:ss";
g.exitTime = dt.ToString(format);
if (ModelState.IsValid)
{
db.Entry(g).State = EntityState.Modified;
db.Entry(g).Property(gg => gg.name).IsModified = false;
db.Entry(g).Property(gg => gg.surname).IsModified = false;
db.Entry(g).Property(gg => gg.firm).IsModified = false;
db.Entry(g).Property(gg => gg.permitNumber).IsModified = false;
db.Entry(g).Property(gg => gg.magazine).IsModified = false;
db.Entry(g).Property(gg => gg.toWhom).IsModified = false;
db.Entry(g).Property(gg => gg.entranceTime).IsModified = false;
db.Entry(g).Property(gg => gg.entranceTimeTime).IsModified = false;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(g);
}
// GET: Guests/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Guest guest = db.Guests.Find(id);
if (guest == null)
{
return HttpNotFound();
}
return View(guest);
}
// POST: Guests/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Guest guest = db.Guests.Find(id);
db.Guests.Remove(guest);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
看法:
@model Guestbook.Models.Guest
@{
ViewBag.Title = @Resources.exitGuest;
}
<h2>@Resources.exitGuest</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ID)
<div class="form-group">
@Html.LabelFor(model => model.name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DisplayFor(model => model.name, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.surname, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DisplayFor(model => model.surname, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.firm, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DisplayFor(model => model.firm, new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
<br />
<div class="form-group">
<div class="col-md-10">
<b>@Resources.leftOk</b>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" [email protected] class="btn btn-default" />
<input type="button" [email protected] onclick="location.href='@Url.Action("","guests")'" class="btn btn-default" />
</div>
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
首先,在您的视图中,您仅输出单个表单变量,即ID参数。所有其他模型属性仅显示。例如,您需要将其输出,EditorFor
或者HiddenFor
将其重新发布。
其次,在您的Edit
方法中,方法签名是这样的:
public ActionResult Edit([Bind(Include = "exitDate,exitTime")] Guest g)
该Bind
属性告诉MVC模型绑定程序仅绑定exitDate
和exitTime
属性,因此其他所有内容都将显示为null
。您可以删除属性:
public ActionResult Edit(Guest g)
或添加您需要传入的其他属性:
public ActionResult Edit([Bind(Include = "exitDate,exitTime,name,surname,etc...")] Guest g)
因此,如果将这两个问题结合在一起,您将看到即使该ID
属性也没有通过,并且所有内容都为空。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句