PurpleSmurph
PurpleSmurph

Reputation: 2107

Delete Issue with MVC 5 List

Recently taken some time away from MVC and come back to an old project, trying to re-write code I've done previously but have come unstuck whilst deleting an item from a list, with EF it's fine but I'm trying not to use Entity Framework to manage my model data. I want to use my model as a database until I'm happy to submit.

I've re-written the issue to simplify it and not dump loads of code, when clicking delete I'm getting the below error:

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult RemoveRequested(Int32)' in 'Project.Views.requestedController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

I think that the id is usually handled by EF but I thought [key] would handle this to auto-increment - is this possible to sort?

Hope this makes sense. Dynamic I don't really care about, so ideally no jQuery/java script unless I have to.

Code:

Partial View

@model IEnumerable<Project.Models.Allocation>
@using (Html.BeginForm())
{
    if (Model != null)
    {
        foreach (var ri in Model)
        {
            <div class="ui-grid-c ui-responsive">
                <div class="ui-block-a">
                    <span>
                        @ri.one
                    </span>
                </div>
                <div class="ui-block-b">
                    <span>
                        @ri.two
                    </span>
                </div>
                <div class="ui-block-c">
                    <span>
                        @ri.three
                    </span>
                </div>
                <div class="ui-block-d">
                    <span>
                        @Html.ActionLink("Delete", "RemoveRequested", new { id = ri.id })
                    </span>
                </div>
            </div>
        }
    }

Models

public class Allocation
    {
        [Key]
        public int? id { get; set; }
        [Required]
        public string one { get; set; }
        [Required]
        public string two { get; set; }
        [Required]
        public string three { get; set; }
    }
public class Container
{
    [key]
    public int? id { get;set; }
    [Required]
    public List<Allocation> requested { get;set; }
}

Controller Action Method

public ActionResult RemoveRequested(int id)
        {
            var newContainer = (Container)Session["containerSession"];
            if(newAllocation.requested != null)
            {
                var del = newContainer.requested.Find(m => m.id == id);
                newContainer.requested.Remove(del);
            }
            Session["containerSession"] = newContainer;
            return RedirectToAction("Index");
        }

Upvotes: 2

Views: 60

Answers (1)

hutchonoid
hutchonoid

Reputation: 33306

I would not have a nullable key and add the [DatabaseGenerated(DatabaseGeneratedOption.Identity)] attribute.

Change your Allocation type change it to:

public class Allocation
{
     [Key]
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
     public int id { get; set; }
     [Required]
     public string one { get; set; }
     [Required]
     public string two { get; set; }
     [Required]
     public string three { get; set; }
}

This will match your action parameter now. However if your key is null you must have other issues and they will now use the default value of zero.

Another option would be to change your action to accept the nullable type as a parameter:

public ActionResult RemoveRequested(int? id)

Pls note I would also use a HttpPost to delete rather than an HttpGet as you are currently doing.

Upvotes: 2

Related Questions