0

I need to pass the value one page javascript to another page javascript particular functionality . I ma using the mvc function the two pages are different controller cshtml pages. How to pass the value one cshtml to another one cshtml. I try this code but not trigger. Please forward your knowledge

view1:

 b.on('click', function () {
          document.location.href = +'/view2.cshtml?Id=' + sData; // notes:
    });

view2:

 var data = $_POST['Id'];
    function work(data) {
    // my code
    }

notes:

I dont know how to mention the particular view2. Because i have lot of controller every controller have the same view2 name. This code goes to the my current controller view 2

6
  • You cannot directly pass the querystring to .cshtml file. Instead you need to pass it to the action name of your controller. Commented Feb 27, 2013 at 5:33
  • Please sent it by answer Commented Feb 27, 2013 at 5:35
  • @GauravVashishtha that is for php, the OP has specified asp.net mvc in the tags Commented Feb 27, 2013 at 5:35
  • vashi please explain clearly Commented Feb 27, 2013 at 5:36
  • dakait do you know answer? please send it Commented Feb 27, 2013 at 5:37

2 Answers 2

1

Try like this:

b.on('click', function () {
      document.location.href = +'/controllerName/View2?Id=' + sData; // notes:
});

On your controller you should be accessing the querystring parameter.

public ActionResult View2(int Id)
{
    ViewData["ID"]=Id; //you can store the Id in ViewData or ViewBag
    return View();
}

Now in your view2.cshtml file, display the ViewDate that you have stored.

 @ViewData["ID"]

Hope it helps

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

3 Comments

but how its use the particular view 2 page function?
@User279_1 Are you cleared with the basics of MVC model? If not then do some google and learn it. The particular view2 function is an "Action" and it will be called automatically with this one line: document.location.href = +'/controllerName/View2?Id=' + sData;
you have wrote jQuery to redirect to a location right ? in your location you need to specify the action name. Am i understanding you correctly ?
0

jQuery Cookie might help: https://github.com/carhartl/jquery-cookie

view 1, set data:

$.cookie('my_cookie', 'sData');

view 2, get data:

$.cookie('my_cookie');

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.