0

I'm developing an application using MVC/Jquery, i able to get a value from controller to view by below getJson code,

 $.getJSON('@Url.Action("SampleData", "Home")', { pageNum: 1, pageSize: PageSize, accountDetailsType: AccountDetailsType }, function (result) {
            //total number of records
            totalRecords = result.total;
            //total records
            records = result.data;

            $('#Description').val(result.reportType); 

$('#Description') gives me desired data, which i can check by below textbox in view,

@Html.TextBox("Description")

now, question is, is there any way to put this value in any variable/hidden filed, so that base on that value..i can show/hide some control in my view...

similar like,

if(("Description") = "VB")
{
}
else
{

}
2
  • Welcome to StackOverflow. Please use the {} button to format your code the next time. I've added additional relevant tags as well. Commented Apr 24, 2011 at 7:55
  • Do you want to access the same data which returned from getJSON ? you are already doing it using $("#Description"). Why do you want to copy the information in another variable/hidden field? Commented Apr 24, 2011 at 8:41

2 Answers 2

1

I don't see no need for putting the result into a hidden field. You can directly hide or show the affected controls in the callback function of your Ajax request:

$.getJSON('@Url.Action("SampleData", "Home")',
    { pageNum: 1, pageSize: PageSize, accountDetailsType: AccountDetailsType },
    function (result) {
        //total number of records
        totalRecords = result.total;
        //total records
        records = result.data;

        if (result.reportType == "VB") {
          $('#control1').hide();
          $('#control2').show();
        } else {
          $('#control1').show();
          $('#control2').hide();
        }
    });
Sign up to request clarification or add additional context in comments.

1 Comment

+1 - bonus pointer, you can turn those 6 lines of hide/show into about 2 by using .toggle instead.
0

you have to change following line

@Html.TextBox("Description")

to

@Html.Hidden("Description")

you can set value of this hidden field like

$("#Description").val(result.reportType);

and read it like

var somevar = $("#Description").val(); //assign to variable or make comparisons

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.