Pages

Thursday, May 23, 2013

Passing String Collection or Array from Controller to View Script in MVC 4

Hi Guys,

This is going to be a short post but thought to just keep this for the record, as I had to search on this for some time.

Scenario:
All I was trying to do was to initialize collection of strings in the controller and wanted to pass this collection to the view.
View was having the JavaScript function which required to have this collection in the form of array.

After searching around this I found a solution which is a single liner :).

Here is my controller where I have simply initialized the string collection and passing through the ViewBag on the view

Controller:


public ActionResult Index()
{
   List<string> entites = new List<string>();
   entites.Add("User 1");
   entites.Add("User 2");
   entites.Add("User 3");
   entites.Add("User 4");

   ViewBag.Users = entites;
   return View();

}


View / JavaScript:

<script>
    $(document).ready(function ()
    {
        var usersArray = @Html.Raw(Json.Encode(ViewBag.Users))
           
        //Some Code..


    });
</script>

Output:

var usersArray = ["User 1","User 2","User 3","User 4"]




Hope this helps someone.

Thursday, May 16, 2013

Error Solved : The specified module Azure was not loaded because no valid module file was found in any module directory

I was trying to configure the TFS build for continuous deployment on Azure Cloud Services by following the steps mentioned in the documentation http://www.windowsazure.com/en-us/develop/net/common-tasks/continuous-delivery/

After configuring everything, when I queued my build - I got this error message - The specified module Azure was not loaded because no valid module file was found in any module directory

As soon as I looked this error I knew that this was coming from the PowerShell file named as - PublishCloudService.ps1 which you can get from the link mentioned above.
After quick search - I got a link which actually resolved the issue.

What was the issue?

The build was trying to launch the process on build server - PowerShell.exe but somehow system was not able to find the AzurePowerShell module path.

Solution:

Open your PowerShell Script file and find the command Import-Module Azure and add the following line just above it
$env:PSModulePath=$env:PSModulePath+";"+"C:\Program Files (x86)\Microsoft SDKs\Windows Azure\PowerShell"

After this modification , save the file and try to queue the build again.

I hope this helps someone. 


Friday, May 3, 2013

Error creating the Web Proxy specified in the 'system.net/defaultProxy' configuration section - Azure Emulator

All right, again a quick post on a strange error I saw in Azure Emulator.

We had our ASP.NET MVC Site up and running when we used to run it in standalone mode.

So for Diagnostic purpose , we used Azure Diagnostic features and configured our web role with the settings which were needed to be done as described in article Here. and right after that we started facing the issue in our application which said

Error creating the Web Proxy specified in the 'system.net/defaultProxy' configuration section

After a long chase, reading few hundred posts on internet there was nothing logical which I could figure unless I came across this similar post.

Solution:

Open Web.config file of your application and check the trace listener's tag , In my case that was missing the filter type attribute.
So the correct tag should be like


<listeners>
 <add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=1.8.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="AzureDiagnostics">
   <filter type=""/>
 </add>
</listeners>

Hope this helps someone.



Server Error 500 Solved : Azure Emulator and ASP.NET MVC

Hi Guys,

A quick post on how we solved the strange issue in Azure Emulator.
We had our ASP.NET MVC site up and running , but when we used to run it in the Azure Emulator - we used to get an error with code 500.

After long research and reading various posts on it , nothing helpful was found.

What was the reason behind this error?

There is a class file named WebRole.cs for any web role project in Azure. This class is inherited from another class known as RoleEntryPoint.
So the WebRole.cs usually contains override of a method known as OnStart().
There are few more methods you can override in this class like OnStop() and OnRun()

In our case the OnRun() method was having empty implementation and was missing base.Run() statement which was causing the problem.

After adding simply one statement i.e. base.Run() in the OnRun() method everything started working fine.

Hope this helps someone.