python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
Convert viewbag to javascript array

Convert MVC 5 WiewBag to JavaScript Array

This is basically used to Convert or cast mvc 5 viewbag to arrayin JavaScript and also compare two strings in jquery. 

Table of Contents

  1. JavaScript Code sample.
  2. MVC 5 Controller Action code sample.
The example as given below.

JavaScript Code sample

<script type="text/javascript">      
    $(function () {
        var array = @Html.Raw(Json.Encode(@ViewBag.ACList))      
        $('#ACNumber').focusout(function () {
            var currentVal = $.trim($('#ACNumber').val()).toLocaleLowerCase();

            $.each(array, function (key, val) {
                var existingVal = $.trim(val).toLocaleLowerCase();

                if (currentVal == existingVal) {
                    kendoDialogForEndUserAlert("Already Exist", "This acccount number is exists in database. <br/> Please re-enter unique account number. <br/>", "error", ["ok"], null);
                    return false;


MVC 5 Contoller code sample

 public ActionResult Create(int id)

    AccountNumberRepository repo = new AccountNumberRepository();
      ViewBag.ACList = repo.GetAccountNumbers(id).ToList();
      return View();


Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - and
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like Powered by Blogger.
ASK Questions