0

I have a javascript function and when it is called I want to insert a partial into a div. All is working fine, but, when I want to pass some javascript into Html.Partial ViewDataDictionary, it isn't passing the rendered javascript.

<script>

function addExperience() {

     @{var uid = @MvcHtmlString.Create("new Date().getTime()");}
     console.info(@uid); //output ok !

     var partialView = @(Html.Raw(JsonConvert.SerializeObject(Html.Partial("~/Views/Dashboard/_editUserExperience.cshtml", Model,  new ViewDataDictionary { { "id", uid } }).ToString().Trim('"'))))

     $("#newExperienceSection").append(partialView); //it renders "new Date().getTime(), not the number
}

</script>

Thank you !

5
  • 1
    Razor code (Html.Raw, Html.Partial) is parsed on the server before its sent to the client. A javascript variable does not even exist at that point - its not in scope. Commented Feb 27, 2015 at 9:51
  • "View Page Source", what does the rendered addExperience() code look like? Commented Feb 27, 2015 at 9:52
  • Unfortunately it is rendered all partial content with "new Date().getTime()" ... Do you know a better method to insert a partial view dynamically ? Commented Feb 27, 2015 at 10:01
  • as Stephen mentioned, Razor is parsed on server and at that time you will not have javascript code, so what you can do is, once your page is loaded you can have an ajax to simply get your partial view and append it in newExperienceSection Commented Feb 27, 2015 at 10:04
  • @RaduGabor: So the problem is you are rendering a string? What happens if you remove ToString().Trim()? This should technically work if you are using Raw and you have the correct content format in your partial. However, I worry what that SerializeObject may be producing, you sure you need that? Commented Feb 27, 2015 at 10:26

1 Answer 1

2

If you call Jsonconvert.SerializeObject with a string (Html.Partial returns a string), it returns a string.

So you statement var partialView = ... will be rendered as

var partialView = "the contents of the partial view";

That's why, when you do this:

$("#newExperienceSection").append(partialView);

it actually displays the javascript as text.

If you want to get a partial view to execute javascript, you can return javascript inside script tags, and as soon as you add that to the DOM it gets executed, for example if you set your _editUserExperience.cshtml as this:

<script>
  alert('this gets executed as soon as you do add this to the DOM with the jQuery append command');
</script>

When you execute $("#newExperienceSection").append(partialView); you'll see the alert.

An easier way to insert a partial view is to take advantage of $.ajax, for example, in addExperience:

$.get('@Url.Action("ActionMethodThatReturnsPartial", "YourController")').done(function(theHtmlReturned) { 
  $("#newExperienceSection").html(theHtmlReturned);
});

($.get is just shorthand for $.ajax using a get request)

Source: http://api.jquery.com/jquery.ajax/

http://api.jquery.com/jquery.get/

http://api.jquery.com/category/deferred-object/

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.