Home > Blockchain >  Get item value from @Html.Raw(Json.Encode(Model)
Get item value from @Html.Raw(Json.Encode(Model)

Time:07-07

I am using @Html.Raw(Json.Encode(Model) to return MVC model items. Now I want to check if a Status is equal to Active and show an alert but I can't seems to make the alert work.

var model = @Html.Raw(Json.Encode(Model));

returns values

var model = [{
  "Id":172,
  "EmployeeNumber":"Email Test ",
  "EmployeeName":"Edward ", 
  "StartDate":"\/Date(1527825600000)\/",
  " EndDate":"\/Date(1870488000000)\/",
  "Status":"Inactive",
 
  "Id":415,
  "EmployeeNumber":"Email Test ",
  "EmployeeName":"Edward ", 
  "StartDate":"\/Date(152782560023)\/",
  " EndDate":"\/Date(19870488000000)\/",
  "Status":"Active",
  // ...
}]

CodePudding user response:

From your description it seems that your model contains list data and you want to display it on the page, know that @HTML.Raw(...) will render HTML markups on HTML correctly. For simple data display you neither need to encode your model nor need to use @HTML.Raw(...) code. Below code should solve your issue i.e.

@{ 
   for (int i; i < Model.Count; i  )
   {
      if(Model[i].Status.Equals("Active"))
      {
         // do something
      }
      else
      {
         // do something
      }
   }
 }
  • Related