0

I have this example code:

artists = data.split(";");
for (var i = 0; i < artists.length; i++) {
  $("#artistsLastStep").append("<div id='" + i + "'>" + '@MyHelper.Username(artists[i])' + "</div>");
}

I need put parameter to MyHelper.Username from javascript array artists.

Error from compiler: "The name 'artists' does not exist in the current context"

What i must to do ?

4
  • possible duplicate of embedding javascript variable within razor syntax Commented Nov 4, 2013 at 15:31
  • This no helps me. They change value in generated string Commented Nov 4, 2013 at 15:58
  • You can't mix javascript and .NET code like that, because when the C# code runs, you're on the server, not the browser, and by the time the javascript runs, you're already disconnected from the server. You have to redesign, possibly using AJAX calls to hit your server from javascript. Commented Nov 4, 2013 at 16:14
  • Could you give me some suggestions ? This is something like a creator. In previously steps, I wrote data to .net session and on last step I need to get all data from session and dynamicly create div's Commented Nov 4, 2013 at 16:18

1 Answer 1

0

Without knowing too much about the problem, I'd say something like:

function populateUsers(artists) {
    jQuery.ajax({
        url: "@Url.ActionLink("GetUsersDivs", "SomeControllerName")",
        dataType: "html",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({artistIds: artists})
    }).done(function(result) {
        jQuery("#artistsLastStep").html(result);
    });
}

This takes your array of artists, makes a single AJAX call to the server, which you'd just need a controller action to handle. The controller does the work of converting an artist ID into a name or object or whatever it is. You could then have a partial view which builds your div for a collection of artists and sends it back on the AJAX call.

Sign up to request clarification or add additional context in comments.

Comments

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.