3

I try to put two variable in JQuery which came from viewbags :

        $("#btnAdd").click(function () {
        var url = dev + "/Legacy/PutContentInThematic";
        var GroupingId = $("#GroupingId_Dialog").val();            
        var Title = $("#Title_Dialog").val();
        var Synopsis = $("#Description_Dialog").val();
        var image = $("#Image_Dialog").val();
        var ThematicId = @ViewBag.thematicid //Here i can't put ';'
        var ThematicName =  @ViewBag.Name

        $.ajax({
            url: url,
            cache: false,
            type: 'POST',
            data: {
                GroupingId: GroupingId,
                ThematicId: ThematicId,
                Title: Title,
                Synopsis: Synopsis,
                Image: image,
                ThematicName: ThematicName
            }
        });
    });

The problem concerns the two last variables "ThematicId" And "ThematicName", it is impossible to put ';' at the end, so the second variable ThematicName don't work.

I try to put this variable out of the function, but i doesn't work anymore. Have you and idea to fix it or an other solution for this Ajax call ?

2 Answers 2

10

You need to write it this way:

var ThematicId = parseInt('@ViewBag.thematicid');
var ThematicName =  '@ViewBag.Name'; 
Sign up to request clarification or add additional context in comments.

5 Comments

This only works if both ViewBag values are strings, what if thematicid is an integer?
@Ashley posted how to do for int
you can also check to see if the variable contains only integer... pietschsoft.com/post/2008/01/14/…
@Julien698 welcome, parseInt() will work, if the value is int otherwise it will not, so consider @Olluwafemi commented link as well which suggests
You're right, but in my case i'm lucky because Thematicid is an int.
3
var ThematicId = @(ViewBag.thematicid); //For int
var ThematicName =  '@ViewBag.Name';   //For string

6 Comments

For the first variable it don't work for me it says that ';' at the end is an error, so i use the solution with parseInt
@Julien698 Where does it say this is an error? The view should be valid with this syntax.
the error is on the ; at the end of the first variable
@Julien698 When you try and run the website what is the error message you are given?
The Website run without error, but is't juste in the page before to run. imageshack.com/a/img673/7188/A3nPwV.jpg
|

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.