Solved

Mixing Javascript and C#

Posted on 2014-10-26
3
129 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 32

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now