2

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.

1
  • 2
    &? That's html encoding, why is it present in the url? Commented Dec 8, 2016 at 10:20

2 Answers 2

1

try this window.location.href = encodeURI(url);

Sign up to request clarification or add additional context in comments.

1 Comment

try this manually first window.location.href = 'http://localhost:2345/Store/Item?parentPartId=1&UserID=1'
0

Check Below code for create URL :

$("#BookName").click(function (event) {
event.preventDefault();
var pathname = window.location.pathname;
var loc = window.location;
var pathName = loc.pathname.substring(0, loc.pathname.lastIndexOf('/') + 1);
// to show it in an alert window
var fullUrl = loc.href.substring(0, loc.href.length - ((loc.pathname + loc.search + loc.hash).length - pathName.length)) + "Item";
window.location = fullUrl + "?parentPartId =" + $(this).data("id")+"&UserID="+$("#USERID").val();
//you can use window.location.href as well
});

Action should be :

[HttpGet]
public ActionResult Item(int? parentPartId,int? UserID)
{

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.