Solved

Mixing Javascript and C#

Posted on 2014-10-26
3
150 Views
Last Modified: 2014-10-27
HI Experts,

in a Razor View I need the INDEX of the loop mixed in a C# syntax something like:

 
<script type="text/javascript">
...
 $.getJSON("/Controller/Action", { id: val }, function (data) {
           $.each(data, function (index, item) {
                   divbody +=    '<input id="' + selected_id + '" checked="@Model.PaymentTyp[index].Selected" type="checkbox" >' 
            });
 });
...
</script>

Open in new window


what would be the right syntax for that?

thanks.
0
Comment
Question by:moshik008
3 Comments
 
LVL 33

Expert Comment

by:shalomc
ID: 40405876
Without diving too deep into your code, it seems that you are on the correct path.

C# is executed server side in IIS. For the C# code, Javascript is just text it outputs.
Javascript is executed client side in the browser. For the Javascript code, C# is completely unknown and transparent.

It is your responsibility to write the C# code that prints the correct JS and HTML.

Sometimes things don't work properly, and that's when you will ask EE for help.

Cheers
0
 
LVL 11

Accepted Solution

by:
louisfr earned 500 total points
ID: 40406035
Here's a way to do it. I don't know if it's the best in your case, but I think it should work.
<script type="text/javascript">
...
        $.getJSON("/Controller/Action", { id: val }, function (data) {
                $.each(data, function (index, item) {
                        var checked = "";
                        switch(index) {
@for(int i = 0; i<Model.PaymentTyp.Length; i++)
{
    if (Model.PaymentTyp[i].Selected) { <text>case @i:<text> }
}
                                checked = "checked";
                        }
                        divbody +=    '<input id="' + selected_id + '" ' + checked + ' type="checkbox">';
                });
        });
...
</script>

Open in new window

Note that the syntax "checked=true" is not the right one. Use either "checked" alone, or checked='checked'.
0
 

Author Closing Comment

by:moshik008
ID: 40406119
Not the cleanest way but hey, it worked.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do I hide MVVM menu items in WPF/XAML? 1 31
Video Tutorial help 2 37
Table style position to the left 8 24
Glitching Slide Show 19 26
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question