model pass outside foreach loop in mvc view code example
Example 1: How to pass a map from controller to javascript function in VF page
public String getButtonNameMap() {
Map<String, Id> buttonNameMap = new Map<String, Id>();
for(LiveChatButton chatButton:[SELECT DeveloperName FROM LiveChatButton WHERE DeveloperName =:buttonNamesSet LIMIT :Limits.getLimitQueryRows() - Limits.getQueryRows()] ) {
buttonNameMap.put(chatButton.DeveloperName,chatButton.Id);
}
return JSON.serialize(buttonNameMap);
}
Example 2: mvc pass model to partial view
@model LetLord.Models.Tenant
<div class="row-fluid">
<div class="span4 well-border">
@Html.Partial("~/Views/Tenants/_TenantDetailsPartial.cshtml", Model)
</div>
</div>
Example 3: MVC view pass model to javascript function
var dto = @Html.Raw(Json.Encode(Model));
Example 4: javascript get last object in foreach loop
arr = [1, 2, 3];
arr.forEach(function(i, idx, array){
if (idx === array.length - 1){
console.log("Last callback call at index " + idx + " with value " + i );
}
});