The parameters dictionary contains a null entry for parameter 'userId' of non-nullable type 'System.Int32' for method

Qvadriologic

Iam having a little problem I need to solve.

I get this error while trying to edit: "The parameters dictionary contains a null entry for parameter 'userId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Index(Int32, Int32, Int32)"

Model:

public partial class Stamping
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Required]
    public int UserId { get; set; }

    [Required]
    [DataType(DataType.DateTime)]
    public DateTime Timestamp { get; set; }

    [Required]
    [StringLength(3)]
    public string StampingType { get; set; }

    public virtual User User { get; set; }
}

View:

@model Aviato.Models.Stamping

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Redigera</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        @Html.HiddenFor(model => model.Id)
        @Html.HiddenFor(model => model.UserId)

        <div class="form-group">
            @Html.LabelFor(model => model.Timestamp, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Timestamp, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Timestamp, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.StampingType, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.StampingType, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.StampingType, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Spara" class="btn btn-default" />
            </div>
        </div>
    </div>
}

Controller:

public ActionResult Edit(int? id)
{
   var stamping = _db.Stampings.Find(id);

   return View(stamping);
}

[HttpPost]
public ActionResult Edit(Stamping stamping)
{
   if (ModelState.IsValid)
   {
       _db.Entry(stamping).State = EntityState.Modified;
       _db.SaveChanges();
       return RedirectToAction("Index");
   }

   return View(stamping);
}

public ActionResult Index(int userId, int year, int weekNo) 
{
  var startTime = DateConverter.FirstDateOfWeek(year, weekNo); 
  var endTime = startTime.AddDays(6); 
  const string msg = "Stampings not found."; 
  var stampings = _db.Stampings.Where(s => s.Timestamp >= startTime && s.Timestamp <= endTime)  .Where(s => s.UserId == userId).ToList(); 
  if (stampings.Count == 0) 
  { 
    return RedirectToAction("GetWeekStamp", new {message = msg}); 
  } 
  return View(stampings); 
}
Mairaj Ahmad

Your index method expects parmeters (int userId, int year, int weekNo) and you are calling it here without paramerters return RedirectToAction("Index"); so it is returning null here

var stampings = _db.Stampings.Where(s => s.Timestamp >= startTime && s.Timestamp <= endTime)  .Where(s => s.UserId == userId).ToList(); 

because userid is not passed to Index method hence no data is returned in stampings and you are passing it to view.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

The parameters dictionary contains a null entry for parameter 'userId' of non-nullable type 'System.Int32' for method

From Dev

MVC : The parameters dictionary contains a null entry for parameter 'k' of non-nullable type 'System.Int32'

From Dev

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32'

From Dev

The parameters dictionary contains a null entry for parameter 'imageWidth' of non-nullable type 'System.Int32'

From Dev

How to solve The parameters dictionary contains a null entry for parameter 'productId' of non-nullable type 'System.Int32'

From Dev

The parameter dictionary contains a null entry for parameter 'IDnumber' of non-nullable type 'System.Int32'

From Dev

dictionary contains a null entry for parameter 'domainType' of non-nullable type

From Dev

Trying to add new data to table leads to an error: The parameters dictionary contains a null entry for parameter 'id' of non-nullable type

From Dev

The parameters dictionary contains a null entry for parameter Orderid

From Dev

Clean way of avoiding "null entry for parameter id of non-nullable type" error for input parameters

From Dev

C#.NET MVC4 - null entry for parameter of non-nullable type for a method

From Dev

The parameters dictionary contains a null entry?

From Dev

The parameters dictionary contains a null entry for parameter error while rendering partial view

From Dev

ASP.net web api 2 error "The parameters dictionary contains a null entry for parameter"

From Dev

ASP.net web api 2 error "The parameters dictionary contains a null entry for parameter"

From Dev

'The parameters dictionary contains a null entry' error, Web API

From Dev

LINQ error: The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type

From Dev

The type 'MyObject' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'Nullable<T>'

From Dev

Property 'Int32 Year' is not defined for type 'System.Nullable`1[System.DateTime]' When comparing year in two dates

From Dev

Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)'

From Dev

'System.Guid' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type

From Dev

Trying to set a non-null string to type 'System.Int32'

From Dev

How to pass nullable type into function that takes a non null type?

From Dev

How to pass in nullable INT type to INSERT INTO parameter

From Dev

Unable to cast object of type System.Int32 to System.Object[] when calling generic method with parameters via reflection

From Java

Pass null to non-nullable Java method from Kotlin

From Dev

'Int32 Userid()' method - The work before with LINQ but has gone to the entity framework

From Dev

Pass NULL from a VB6 window to a .NET nullable Int32

From Dev

MVC: The model item passed into the dictionary is of type System.Int32

Related Related

  1. 1

    The parameters dictionary contains a null entry for parameter 'userId' of non-nullable type 'System.Int32' for method

  2. 2

    MVC : The parameters dictionary contains a null entry for parameter 'k' of non-nullable type 'System.Int32'

  3. 3

    The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32'

  4. 4

    The parameters dictionary contains a null entry for parameter 'imageWidth' of non-nullable type 'System.Int32'

  5. 5

    How to solve The parameters dictionary contains a null entry for parameter 'productId' of non-nullable type 'System.Int32'

  6. 6

    The parameter dictionary contains a null entry for parameter 'IDnumber' of non-nullable type 'System.Int32'

  7. 7

    dictionary contains a null entry for parameter 'domainType' of non-nullable type

  8. 8

    Trying to add new data to table leads to an error: The parameters dictionary contains a null entry for parameter 'id' of non-nullable type

  9. 9

    The parameters dictionary contains a null entry for parameter Orderid

  10. 10

    Clean way of avoiding "null entry for parameter id of non-nullable type" error for input parameters

  11. 11

    C#.NET MVC4 - null entry for parameter of non-nullable type for a method

  12. 12

    The parameters dictionary contains a null entry?

  13. 13

    The parameters dictionary contains a null entry for parameter error while rendering partial view

  14. 14

    ASP.net web api 2 error "The parameters dictionary contains a null entry for parameter"

  15. 15

    ASP.net web api 2 error "The parameters dictionary contains a null entry for parameter"

  16. 16

    'The parameters dictionary contains a null entry' error, Web API

  17. 17

    LINQ error: The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type

  18. 18

    The type 'MyObject' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'Nullable<T>'

  19. 19

    Property 'Int32 Year' is not defined for type 'System.Nullable`1[System.DateTime]' When comparing year in two dates

  20. 20

    Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)'

  21. 21

    'System.Guid' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type

  22. 22

    Trying to set a non-null string to type 'System.Int32'

  23. 23

    How to pass nullable type into function that takes a non null type?

  24. 24

    How to pass in nullable INT type to INSERT INTO parameter

  25. 25

    Unable to cast object of type System.Int32 to System.Object[] when calling generic method with parameters via reflection

  26. 26

    Pass null to non-nullable Java method from Kotlin

  27. 27

    'Int32 Userid()' method - The work before with LINQ but has gone to the entity framework

  28. 28

    Pass NULL from a VB6 window to a .NET nullable Int32

  29. 29

    MVC: The model item passed into the dictionary is of type System.Int32

HotTag

Archive