1

一般来说,我对 MVC 4 和 ASP.net 比较陌生。我正在尝试创建一个具有一些属性的“房间”,其中一个查询来自另一个模型中包含的状态列表。我的课程设置如下:

房间.cs

public class Room
    {
        [Key]
        public Guid RoomId { get; set; }
        [Required(ErrorMessage = "A Room Name is required")]
        public string Name { get; set; }
        public string Description { get; set; }
        public virtual Status Status { get; set; }
        [DisplayName("Created On")]
        public DateTime? Created { get; set; }
        [DisplayName("Last Modified")]
        public DateTime? Modified { get; set; }
    }

状态.cs

public class Status
    {
        [Key]
        public int StatusId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
    }

当我使用默认脚手架对房间进行创建操作时,我正在使用以下代码,该代码运行良好,并将“新建”状态正确应用于新创建的房间。

// 发布:/房间/创建

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Room room)
        {
            if (ModelState.IsValid)
            {
                room.Created = DateTime.Now;
                room.Status = db.Statuses.Single(s => s.Name == "New");
                room.RoomId = Guid.NewGuid();
                db.Rooms.Add(room);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(room);
        }

当我尝试在编辑操作上分配 room.Status 时使用相同的语法,在我的情况下,自动将状态从“新建”更改为“发现”,状态值永远不会在数据库中修改。我正在追踪当地人直到 db.SaveChanges(); 调用,我可以看到 room.Status 值准确地反映了新状态的定义,但它根本没有在后端更新。这是我的更新代码:

//房间/编辑/5

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Room room)
        {
            if (ModelState.IsValid)
            {
                room.Modified = DateTime.Now;
                room.Status = db.Statuses.Single(s => s.Name == "Discovery");
                db.Entry(room).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(room);
        }

我不知道如何改变这个值!我的编辑视图如下:

@model vwr.Cloud.Models.Room

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Room - @Html.ValueFor(model => model.Name)</legend>

        @Html.HiddenFor(model => model.RoomId)
        @Html.HiddenFor(model => model.Name)
        @Html.HiddenFor(model => model.Created)
        @Html.HiddenFor(model => model.Status.StatusId)

        <div class="editor-label">
            Room Status - @Html.ValueFor(model => model.Status.Name)
        </div>
        <div class="editor-label">
            Created Date - @Html.ValueFor(model => model.Created)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Description)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Description)
            @Html.ValidationMessageFor(model => model.Description)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>
4

2 回答 2

0

我花了很长时间才弄清楚这一点,但我还需要向映射到导航属性的 Room 类添加一个 [ForeignKey] 条目。我不能说我还理解它是如何工作的,我只想说,它确实:-)。

最后的房间.cs

public class Room
    {
        [Key]
        public Guid RoomId { get; set; }
        [Required(ErrorMessage = "A Room Name is required")]
        public string Name { get; set; }
        public string Description { get; set; }

        [ForeignKey("Status")]
        public virtual int StatusId { get; set; }

        public virtual Status Status { get; set; }
        [DisplayName("Created On")]
        public DateTime? Created { get; set; }
        [DisplayName("Last Modified")]
        public DateTime? Modified { get; set; }
    }
于 2013-08-13T16:03:50.617 回答
0
public class Room
{
    [Key]
    public Guid RoomId { get; set; }
    [Required(ErrorMessage = "A Room Name is required")]
    public string Name { get; set; }
    public string Description { get; set; }

    [ForeignKey("Status")]
    public virtual int StatusId { get; set; }

    public virtual Status Status { get; set; }
    [DisplayName("Created On")]
    public DateTime? Created { get; set; }
    [DisplayName("Last Modified")]
    public DateTime? Mod
于 2013-12-20T09:29:20.160 回答