0

I am going crazy searching for it, but I can,t find any solution to my problem. All I want is to set Session in razor with value of javascript variable.

My Code is:

  function SetParameter() {
    var OrderShipmentInd = "";
    if ($("#rdoOrd").attr("checked") == "checked")
        OrderShipmentInd = "O";
    else
        OrderShipmentInd = "S";

    @Session["OrderShipmentInd"] = OrderShipmentInd;
}

I get error "Conditional Compilation is turned off". when I use /*@("@cc_on @")*/ to remove this error, it does not work. my page does not renders.

Please anyone help, how to do this. Does this world has any experts?

1
  • 1
    (Assuming this is client side js), you will need to Post or Ajax the OrderShipmentInd back to the server in order to store it in Session. Commented Jun 19, 2013 at 5:45

1 Answer 1

1

You can't do that.

Session is run on the server, but javascript is run on client. To get that value into a session, you should use either an ajax get/post into a controller action, or post the whole into a controller/action and add it there.

e.g.

Javascript:

function SetParameter() {
    var OrderShipmentInd = "";
    if ($("#rdoOrd").attr("checked") == "checked")
        OrderShipmentInd = "O";
    else
        OrderShipmentInd = "S";

    $.get ('/OrderShipment', { order: OrderShipmentInd });
}

MVC Action:

public ActionResult OrderShipment(string order)
{
      @Session["OrderShipmentInd"] = order;
      //..
}
Sign up to request clarification or add additional context in comments.

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.