Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi ,

I'm using a JQuery Grid in MVC Application. I cannot assign or compare javascript value with model values the following is the code related to it.

1. function (value) {
2.        var  val=value;
3.        @foreach (var item in Model.ToList())
4.         {
5.       //val=@item.FKAlbumId
6.             if (@item.FKAlbumId ==Convert.ToInt32(val.ToString()))
7.             {
8.                    return @item.Album.Title;
9.             }
10.    }

The following are the errors I get:
line 6: the name val doesn't exist in the current context.
line8: Since 'System.Web.WebPages.SectionWriter' returns void, a return keyword must not be
followed by an object expression.

Please help me in sorting out this issue.
Sorry if my english is poor.
Thanks in Advance.

With Regards,
Rohith.
Posted

1 solution

You are trying to run server-side code alongside client-side code this will not work

C#
/*this is client-side code*/
 function (value) {
  var  val=value;
/*this is client-side code*/

/*this is server-side code - it will run when page is being rendered but will not exists in the client browser */
  @foreach (var item in Model.ToList())
   {
       //val=@item.FKAlbumId
        /* val still not exists and will only do when javascript code be evaluated in the browser*/
       if (@item.FKAlbumId == Convert.ToInt32(val.ToString()))
       {
              return @item.Album.Title;
       }
    }
/*this is server-side code*/
  }


and give your function a name if you want to call it.

Hope it helps.
And sorry, my english is bad too
 
Share this answer
 
Comments
Rohith Reddy Vadiyala 6-Apr-13 0:38am    
Thanks @Moykn. Thanks for your suggestion..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900