[英]Why is my input type="file" always returning null to the viewmodel in ASP.NET MVC and Entity Framework
[英]Why is my (id == null) returning null on my update method in Entity Framework MVC?
好的,所以我已經發布了我在該項目中正在做的一些事情。 我還在學習。 無論如何,當我的控制器正在調用我的update方法的視圖時。 我的id參數為null。 我在ActionResult Edit動作上以及運行它時在if(id == 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; }
}
}
假設您要從某個頁面調用“編輯”,那么您的“操作”鏈接將如下所示,
@Html.ActionLink("Edit", "Edit", "Music", new{ id = value })
在這里,第一個Edit只是一個Name。 第二個Edit是您的操作方法名稱,Music是您的控制器名稱,其中存在Edit Action方法。
在您的Song模型類中,使Id可為空。
public int? Id { get; set; }
在您的問題“ Edit.cshtml”中,您正在向HttpGet方法提交表單。 應該是HttpPost方法。 而且您需要在“編輯”表單中將id包含為隱藏字段(如果您不想顯示),例如波紋管,
@Html.HiddenFor(m => m.Id) //This is your Song Id
這將在“編輯”視圖中的窗體的大括號}之前。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.