捕获从控制器返回的json值到asp.net mvc视图中的变量

时间:2022-11-30 18:01:14

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

我正在使用MVC / Jquery开发一个应用程序,我能够通过getJson代码从控制器获取一个值,

 $.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,

$('#Description')为我提供了所需的数据,我可以在下面的文本框中查看,

@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...

现在,问题是,有没有办法将此值放在任何变量/隐藏字段中,以便基于该值... i可以在我的视图中显示/隐藏某些控件...

similar like,

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

}

2 个解决方案

#1


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:

我认为没有必要将结果放入隐藏的字段中。您可以直接隐藏或显示Ajax请求的回调函数中受影响的控件:

$.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();
        }
    });

#2


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

#1


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:

我认为没有必要将结果放入隐藏的字段中。您可以直接隐藏或显示Ajax请求的回调函数中受影响的控件:

$.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();
        }
    });

#2


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