简体   繁体   中英

Why is my (id == null) returning null on my update method in Entity Framework MVC?

Ok, so I've posted a few things that I'm doing with this project. I'm still learning. Anyway, my controller when it is calling the view for my update method. I'm getting null for the id parameter. I set a breakpoint on if(id == null) on my ActionResult Edit action, and when I run it; it shows null within the breakpoint autos window. Can someone please tell me why I'm getting null when there is data in my database?

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; }
}
}

Suppose you are calling Edit from some page, then your Action link will be like this,

@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.

In your Song model class, make Id nullable.

public int? Id { get; set; }

In your question,in "Edit.cshtml", you are submitting a form to HttpGet method. It should be HttpPost method. And you need to include id in your Edit form as hidden field(if you dont want to show), like bellow,

@Html.HiddenFor(m => m.Id) //This is your Song Id

This will be before close braces } of form in Edit view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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