I'm new in Asp.Net MVC write this java script code in view page for create url action:
$("#BookName").click(function (event) {
event.preventDefault();
var url = '@Url.Action("Item", "Store", new {parentPartId = "PARENT_ID",UserID="USER_ID"})';
url = url.replace("USER_ID", $("#USERID").val());
url = url.replace("PARENT_ID", $(this).data("id"));
alert(url); //just for debugging
window.location.href = url;
});
and alert(url); //just for debugging show me this url:
http://localhost:2345/Store/Item?parentPartId=1&UserID=1
And this is my Item action:
public ActionResult Item(int parentPartId,int UserID)
{
StoreOnlineEntities1 storeOnline = new StoreOnlineEntities1();
var query_find = (from p in storeOnline.BookDetails
where p.BookID == parentPartId
select new
{
p.BookID,
p.Exaplain
}).ToArray();
ViewBag.Detail = query_find[0].Exaplain;
ViewBag.BookID = query_find[0].BookID;
ViewBag.UserID = UserID;
return View();
}
but when run my app get this error:
The parameters dictionary contains a null entry for parameter 'UserID' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Item(Int32, Int32)' in 'StoreProject.Controllers.StoreController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
How can i solve problem?thanks.
&? That's html encoding, why is it present in the url?