简体   繁体   中英

How to create var in Javascript and access in MVC razor

Javascript

 function Content(data) {
        var array = data.split(',');

    var id = array[0]; //I want to save this var and make it 
                       //accessible in the below razor code in the forloop. 
 }

@foreach (Chart item in Model)
 {
    @if(id -- this var is taken from the top javascript.) { 
        <tr>
           <td>                              
               @item.Description
           </td>
        </tr>
    } else { }
 }

As above I want to create a variable or some entity which is set in Javascript which can then be called in razor in the foreach loop.

您的图表项目模型应具有ID ...

Your can do it, only through ajax request.

in the client side use jQuery $.ajax

Controller:

[HttpPost]
        public void ActionName(bool id)
        {
    any actions
           ViewBag.id=id
           return View();
        }

in view:

@foreach (Chart item in Model)
 {
    @if(ViewBug.id) { 
        <tr>
           <td>                              
               @item.Description
           </td>
        </tr>
    } else { }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM