0
votes

I am creating a MVC application. I am passing some data between views like this:

public ActionResult AddGroup(AddGroupViewModel model)
        {
            var entities = new ClassDeclarationsDBEntities1();
            var model1 = new AddGroupViewModel();
            model1.Subjects = entities.Subjects.ToList();
            model1.Users = entities.Users.ToList();
            if (ModelState.IsValid)
            {
                var subj = entities.Subjects
                    .Where(b => b.name == model.subject_name)
                    .FirstOrDefault();
                int id = subj.class_id;
                return RedirectToAction("AddGroupsQty", "Account", new { qty = model.qty, subject_id = id});
            }
            return View(model1);
        }

And:

public ActionResult AddGroupsQty(int qty, int id)
{
    ClassDeclarationsDBEntities1 entities = new ClassDeclarationsDBEntities1();

    var model = new AddGroupsQtyViewModel();
    model.subject_id = id;
    model.qty = qty;
    ClassDeclarationsDBEntities1 entities1=new ClassDeclarationsDBEntities1();
    var subj = entities1.Subjects
            .Where(b => b.class_id == model.subject_id)
            .FirstOrDefault();
    model.subject_name = subj.name;
    return View(model);
}

But unfortunately, I get this error:

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult AddGroupsQty(Int32, Int32)' in 'ClassDeclarationsThsesis.Controllers.AccountController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Nazwa parametru: parameters
enter image description here How do I go about this?

1

1 Answers

6
votes

Your action method AddGroupsQty has 2 parameters, qty and id, But when you do the redirect,your querystring does not have the id one! Instead it has one called subject_id

To fix the error, change your route value item from subject_id to id

return RedirectToAction("AddGroupsQty", "Account", new { qty = model.qty, id = id});

Or you can change the AddGroupsQty action method parameter from id to subject_id (make sure to check all other places and fix the querystring/route values as needed)