好的,所以我发布了一些我正在做这个项目的事情。我还在学习。无论如何,我的控制器在调用我的更新方法的视图时。我为id参数获取null。我在ActionResult Edit操作上设置if(id == null)的断点,当我运行它时;它在断点自动窗口中显示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; }
}
}
答案 0 :(得分:1)
假设您从某个页面调用Edit,那么您的Action链接将是这样的,
@Html.ActionLink("Edit", "Edit", "Music", new{ id = value })
这里的第一个Edit只是一个名字。第二个编辑是您的操作方法名称,音乐是控制器名称,其中存在编辑操作方法。
在您的Song模型类中,使Id可为空。
public int? Id { get; set; }
在您的问题中,在“Edit.cshtml”中,您要向
@Html.HiddenFor(m => m.Id) //This is your Song Id
这将在编辑视图中的表单的紧密括号} 之前。