0

I am Trying to Get TempData Value in Variable by using jQuery

$("#save").click(function () {
  alert("ok")
  var tran = '@TempData["m_pran"]';
  @*var ptran1 = $('@TempData["m_pran"]').val().trim();*@
  if (tran) {
    alert(tran)
  } else {
    alert("Not Get tran ");
  }
});
1
  • That looks correct to me. Are you using this in external js file or .cshtml file? Commented Mar 23, 2019 at 8:39

2 Answers 2

1

You need to put parenthesis around the value as:

$("#save").click(function () {
  alert("ok")
  var tran = '@(TempData["m_pran"])';

  if (tran) {
    alert(tran)
  } else {
    alert("Not Get tran ");
  }
});
Sign up to request clarification or add additional context in comments.

Comments

1

It is correct if your script in cshtml file. If you separate the script to script file.

You should use input tag with type hidden to store temp value.

In cshtml file

<input type="hidden" id="m_pran" value="@TempData["m_pran"]"/>

In js file

$("#save").click(function () {
  alert("ok")
  var tran = $('#m_pran').val();

  if (tran) {
    alert(tran)
  } else {
    alert("Not Get tran ");
  }
});

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.