Solved

Mixing Javascript and C#

Posted on 2014-10-26
3
154 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

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…
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…

738 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