Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't pass ViewBag data to AngularJS

I have a controller:

public ActionResult Edit(int id) {
    ViewBag.IsComplete = false;
    return View(dbContext.Users.Where(user => user.Id == id))
}

and the corresponding view:

@model User

@{
    ViewBag.Title = "Edit User";
    Layout = "~/Views/Shared/_PopupLayout.cshtml";
}

<p>@ViewBag.IsComplete</p>

<div ng-init="init(isComplete: @ViewBag.IsComplete)" />

The first instance of ViewBag.IsComplete (in the <p> tag) emits the correct value. The second emits null. Can anyone tell me what is going on here and how to fix it so the second @ViewBag.IsComplete emits the same as the first?

like image 568
DrewB Avatar asked Feb 22 '26 09:02

DrewB


1 Answers

Use @Html.Raw(ViewBag.IsComplete) in your ng-init binding.

like image 104
Dandy Avatar answered Feb 24 '26 03:02

Dandy