当我想在ASP.NET MVC中编辑数据库时,值为null

时间:2014-10-24 07:59:30

标签: asp.net-mvc

我在ASP.NET Model-View-Controller中的项目有问题。

我有一个应用程序“留言簿”。我可以成功创建访客,但我无法编辑它们。

查找: 我开始申请 - 一切都好 2.我创建一个客人 - 一切正常(客人拥有所有输入的数据)
3.我点击“编辑” - 一切正常 我看到了新的窗口。屏幕上的所有字段都有正确的数据,但我在编辑功能中有一个断点,我看到所有变量都为空。

型号:

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" value=@Resources.y class="btn btn-default" />
                <input type="button" value=@Resources.cancel onclick="location.href='@Url.Action("","guests")'" class="btn btn-default" />
            </div>
        </div>
    </div>
}
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

2 个答案:

答案 0 :(得分:2)

首先,在您的视图中,您只输出一个表单变量,即ID参数。仅显示所有其他模型属性。您需要输出它们,例如EditorForHiddenFor,以便将其发回。

其次在您的Edit方法中,方法签名是:

public ActionResult Edit([Bind(Include = "exitDate,exitTime")] Guest g)

Bind attribute告诉MVC模型绑定器仅绑定exitDateexitTime属性,因此其他所有内容都将显示为null。您可以删除属性:

public ActionResult Edit(Guest g)

或者添加您需要传递的其他属性:

public ActionResult Edit([Bind(Include = "exitDate,exitTime,name,surname,etc...")] Guest g)

因此,如果你将这两个问题结合起来,你会发现即使ID属性也没有被传递,一切都是空的。

答案 1 :(得分:2)

使用

 @Html.TextBoxFor(... 

而不是

 @Html.DisplayFor(.... 

这样,视图将在表单提交后将它们传递给控制器​​'edit'HttpPost ActionResult。


 @Html.DisplayFor(...

仅用于将模型输出到屏幕。但是,它不允许以这种方式进行编辑(因此请改用TextBoxForEditorFor


我不太确定您是否希望手动编辑“exitDate”和exitTime,但如果您这样做,则需要将它们添加到视图中,如以及将它们添加到模型后将它们包含在Binding Property中。

如果你这样做,那么使用类似的东西:

(Guest  guest) 

而不是绑定数据

应该包含绑定。