Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Mixing Javascript and C#

Posted on 2014-10-26
3
Medium Priority
?
173 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 1500 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

704 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