I have not changed my code, which used to work, I even clarified this with an earlier build of the project. However, I now get this error:
The parameters dictionary contains a null entry for parameter ‘recipeID’ of non-nullable type ‘System.Int32’ for method ‘System.Web.Mvc.ActionResult Create(Int32, BareCupboard.Models.RecipeStep)’ in ‘BareCupboard.Controllers.RecipeStepController’. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
My code is:
[HttpPost]
public ActionResult Create(int recipeID, RecipeStep newRecipeStep)
{
try
{
var recipe = db.Recipes.Single(r => r.recipeID == recipeID);
recipe.RecipieSteps.Add(newRecipeStep);
db.SaveChanges();
return RedirectToAction("Index", "Recipe");
}
catch
{
return View();
}
}
I tried: int? recipeID, but this fails to work.
Any ideas what might have happened as all I can see is mysticism at play here!
Check your view code for the order of the parameters. The model binder needs it come in proper order. It is very easy to make mistake there.
Update
Here one way of resolving this. Create a view model as below:
In the controller you will have the following:
In the view you can do the following:
Notice the order. I put the id first and then rest of the recipe step data so it gets bind properly.
For the begin form, You dont have to specify action and controller as you are doing the post on the same page. It doesnt hurt if you put there.
Hope this helps… 🙂