为什么我的 (id == null) 在 Entity Framework MVC 中的更新方法中返回 null?
Why is my (id == null) returning null on my update method in Entity Framework MVC?
好的,我已经发布了我在这个项目中所做的一些事情。我还在学习。无论如何,我的控制器在为我的更新方法调用视图时。我的 id 参数为空。我在 ActionResult Edit 操作的 if(id == null) 上设置了一个断点,当我 运行 它时;它在断点自动 window 中显示为 null。有人可以告诉我为什么当我的数据库中有数据时我会得到空值吗?
MusicController.cs
// GET: Songs/Edit/5
[HttpPost]
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Song song = db.Songs.Find(id.Value);
if (song == null)
{
return HttpNotFound();
}
return View(song);
}
Edit.cshtml
@model MusicManager.Models.Song
@{
ViewBag.Title = "Edit Songs";
}
<h2>@ViewBag.Title</h2>
@using (Html.BeginForm())
{
<div class="row">
<div class="col-md-6">
<div class="form-group">
@Html.HiddenFor(m => m.Id)
@Html.LabelFor(m => m.SongName, new { @class = "control-label" })
@Html.TextAreaFor(m => m.SongName, new { @class = "form-control", @placeholder = "Enter song name here" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.ArtistName, new { @class = "control-label" })
@Html.TextBoxFor(m => m.ArtistName, new { @class = "form-control", @placeholder = "Enter artist name here" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.AlbumName, new { @class = "control-label" })
@Html.TextBoxFor(m => m.AlbumName, new { @class = "form-control", @placeholder = "Enter ablum name here" })
</div>
<div>
@Html.LabelFor(m => m.Duration, new { @class = "control-label" })
@Html.TextBoxFor(m => m.Duration, new { @class = "form-control", @placeholder = "Enter duration as x.xx" })
</div>
<div class="form-group">
<div class="checkbox">
<label>
@Html.CheckBoxFor(m => m.Exclude) @Html.DisplayNameFor(m => m.Exclude)
</label>
</div>
</div>
</div>
</div>
<div class="col-md-12">
<div class="pad-top">
<button type="submit" class="btn btn-success btn-lg margin-right">
<span class="glyphicon glyphicon-save"></span> Save
</button>
<a href="@Url.Action("Music")" class="btn btn-warning btn-lg">
<span class="glyphicon glyphicon-remove"></span> Cancel
</a>
</div>
</div>
}
Index.cshtml
@model List<MusicManager.Models.Song>
@{
ViewBag.Title = "Music";
}
<div id="addButton">
<button type="button" class="btn btn-success btn-lg margin-right"><span class="glyphicon glyphicon-plus"></span>@Html.ActionLink("Add", "Add", "Music", null, new { @class="addButton" })</button>
</div>
<h3>@ViewBag.Message</h3>
<table class="table">
<tr>
<th>No.</th>
<th>Song</th>
<th>Artist</th>
<th>Album</th>
<th>Duration</th>
<th> </th>
</tr>
@foreach (var song in Model)
{
<tr>
<td>@song.Id</td>
<td>@song.SongName</td>
<td>@song.ArtistName</td>
<td>@song.AlbumName</td>
<td>@song.Duration.ToString("0.00")</td>
<td>
<div class="pull-right">
<button class="btn btn-warning btn-sm margin-right"><span class="glyphicon glyphicon-edit"></span><span class="hidden-xs">@Html.ActionLink("Edit", "Edit", "Music", new { id = song.Id })</span></button>
<a href="@Url.Action("Delete", new { id = song.Id })" class="btn btn-danger btn-sm">
<span class="glyphicon glyphicon-trash"></span><span class="hidden-xs"> Delete</span>
</a>
</div>
</td>
</tr>
}
</table>
Song.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace MusicManager.Models
{
public class Song
{
/// <summary>
/// Default constructor
/// </summary>
public Song()
{ }
/// <summary>
/// The Id of the song.
/// </summary>
public int Id { get; set; }
/// <summary>
/// The name of the song.
/// </summary>
[Required]
[DisplayName("Song")]
public string SongName { get; set; }
/// <summary>
/// The artist of the song.
/// </summary>
[Required, StringLength(100)]
[DisplayName("Artist")]
public string ArtistName { get; set; }
/// <summary>
/// Represents an album from an artist.
/// </summary>
[Required, StringLength(50)]
[DisplayName("Album")]
public string AlbumName { get; set; }
/// <summary>
/// The duration of the song.
/// </summary>
public double Duration { get; set; }
/// <summary>
/// Whether or not this song should be excluded when calculating the total duration of the current playlist.
/// </summary>
public bool Exclude { get; set; }
}
}
假设您从某个页面调用 Edit,那么您的 Action link 将是这样的,
@Html.ActionLink("Edit", "Edit", "Music", new{ id = value })
Here first Edit is just a Name. Second Edit is your Action Method name and Music is your controller name where Edit Action method present.
在您的 Song 模型中 class,使 Id 可为空。
public int? Id { get; set; }
在您的问题中,在 "Edit.cshtml" 中,您正在向 HttpGet 方法提交表单。它应该是 HttpPost 方法。你需要在你的编辑表单中包含 id 作为隐藏字段(如果你不想显示),如下所示,
@Html.HiddenFor(m => m.Id) //This is your Song Id
这将在编辑视图中 form 的右括号 } 之前。
好的,我已经发布了我在这个项目中所做的一些事情。我还在学习。无论如何,我的控制器在为我的更新方法调用视图时。我的 id 参数为空。我在 ActionResult Edit 操作的 if(id == null) 上设置了一个断点,当我 运行 它时;它在断点自动 window 中显示为 null。有人可以告诉我为什么当我的数据库中有数据时我会得到空值吗?
MusicController.cs
// GET: Songs/Edit/5
[HttpPost]
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Song song = db.Songs.Find(id.Value);
if (song == null)
{
return HttpNotFound();
}
return View(song);
}
Edit.cshtml
@model MusicManager.Models.Song
@{
ViewBag.Title = "Edit Songs";
}
<h2>@ViewBag.Title</h2>
@using (Html.BeginForm())
{
<div class="row">
<div class="col-md-6">
<div class="form-group">
@Html.HiddenFor(m => m.Id)
@Html.LabelFor(m => m.SongName, new { @class = "control-label" })
@Html.TextAreaFor(m => m.SongName, new { @class = "form-control", @placeholder = "Enter song name here" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.ArtistName, new { @class = "control-label" })
@Html.TextBoxFor(m => m.ArtistName, new { @class = "form-control", @placeholder = "Enter artist name here" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.AlbumName, new { @class = "control-label" })
@Html.TextBoxFor(m => m.AlbumName, new { @class = "form-control", @placeholder = "Enter ablum name here" })
</div>
<div>
@Html.LabelFor(m => m.Duration, new { @class = "control-label" })
@Html.TextBoxFor(m => m.Duration, new { @class = "form-control", @placeholder = "Enter duration as x.xx" })
</div>
<div class="form-group">
<div class="checkbox">
<label>
@Html.CheckBoxFor(m => m.Exclude) @Html.DisplayNameFor(m => m.Exclude)
</label>
</div>
</div>
</div>
</div>
<div class="col-md-12">
<div class="pad-top">
<button type="submit" class="btn btn-success btn-lg margin-right">
<span class="glyphicon glyphicon-save"></span> Save
</button>
<a href="@Url.Action("Music")" class="btn btn-warning btn-lg">
<span class="glyphicon glyphicon-remove"></span> Cancel
</a>
</div>
</div>
}
Index.cshtml
@model List<MusicManager.Models.Song>
@{
ViewBag.Title = "Music";
}
<div id="addButton">
<button type="button" class="btn btn-success btn-lg margin-right"><span class="glyphicon glyphicon-plus"></span>@Html.ActionLink("Add", "Add", "Music", null, new { @class="addButton" })</button>
</div>
<h3>@ViewBag.Message</h3>
<table class="table">
<tr>
<th>No.</th>
<th>Song</th>
<th>Artist</th>
<th>Album</th>
<th>Duration</th>
<th> </th>
</tr>
@foreach (var song in Model)
{
<tr>
<td>@song.Id</td>
<td>@song.SongName</td>
<td>@song.ArtistName</td>
<td>@song.AlbumName</td>
<td>@song.Duration.ToString("0.00")</td>
<td>
<div class="pull-right">
<button class="btn btn-warning btn-sm margin-right"><span class="glyphicon glyphicon-edit"></span><span class="hidden-xs">@Html.ActionLink("Edit", "Edit", "Music", new { id = song.Id })</span></button>
<a href="@Url.Action("Delete", new { id = song.Id })" class="btn btn-danger btn-sm">
<span class="glyphicon glyphicon-trash"></span><span class="hidden-xs"> Delete</span>
</a>
</div>
</td>
</tr>
}
</table>
Song.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace MusicManager.Models
{
public class Song
{
/// <summary>
/// Default constructor
/// </summary>
public Song()
{ }
/// <summary>
/// The Id of the song.
/// </summary>
public int Id { get; set; }
/// <summary>
/// The name of the song.
/// </summary>
[Required]
[DisplayName("Song")]
public string SongName { get; set; }
/// <summary>
/// The artist of the song.
/// </summary>
[Required, StringLength(100)]
[DisplayName("Artist")]
public string ArtistName { get; set; }
/// <summary>
/// Represents an album from an artist.
/// </summary>
[Required, StringLength(50)]
[DisplayName("Album")]
public string AlbumName { get; set; }
/// <summary>
/// The duration of the song.
/// </summary>
public double Duration { get; set; }
/// <summary>
/// Whether or not this song should be excluded when calculating the total duration of the current playlist.
/// </summary>
public bool Exclude { get; set; }
}
}
假设您从某个页面调用 Edit,那么您的 Action link 将是这样的,
@Html.ActionLink("Edit", "Edit", "Music", new{ id = value })
Here first Edit is just a Name. Second Edit is your Action Method name and Music is your controller name where Edit Action method present.
在您的 Song 模型中 class,使 Id 可为空。
public int? Id { get; set; }
在您的问题中,在 "Edit.cshtml" 中,您正在向 HttpGet 方法提交表单。它应该是 HttpPost 方法。你需要在你的编辑表单中包含 id 作为隐藏字段(如果你不想显示),如下所示,
@Html.HiddenFor(m => m.Id) //This is your Song Id
这将在编辑视图中 form 的右括号 } 之前。