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 !
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.addExperience()code look like?newExperienceSectionToString().Trim()? This should technically work if you are usingRawand you have the correct content format in your partial. However, I worry what thatSerializeObjectmay be producing, you sure you need that?