繁体   English   中英

为什么我的(id == null)在Entity Framework MVC中的更新方法上返回null?

[英]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>&nbsp;</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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM