Tuesday, 29 July 2014

Pararell Universe Planets

After successful reception of Solar System asset that I published to Unity3d store I decided to create some more planets, this time imaginary that might exists somewhere in the universe.

Solar system still available here

First teaser of new planets


Submitting model with entities in MVC using DataTable fails when using pagination

I have uncommon issue today while trying to submit model with entities and using dataTable plugin.
Model was loosing entities data when user used pagination provided by dataTable plugin.

I post question on stackoverflow, unfortunately I did not get any answer but few hours later I found some small hack and it works now.

If anyone is interested I post it on stackoverflow:

http://stackoverflow.com/questions/25017597/submitting-model-with-entities-in-mvc-using-datatable-fails-when-using-paginatio/25020152#25020152


Tuesday, 8 July 2014

Get the values from http query string variables in View in ASP.NET MVC

This method can be used in any view but it is recommended to make sure your ViewModel contains all necessary information. We are going to use this approach in layout page, as normally layout page does not have any model(it can have but then it might get very messy and personally I think for big projects it could be extra constraint).

Consider a situation in which you are interested in values for variables from http request.

http://localhost:49853/Home/Index?searchType=global

In above address we are passing variable searchType. Let's assume that searchType can have 2 different values - local and global.

In your main css file:

#mainNav.global {
  background: #404939; }
  #mainNav.global nav.nav {
    background: none; }
    #mainNav.global nav.nav .top {
      background: #404939; }
    #mainNav.global nav.nav a:hover, #mainNav nav.nav a.active {
      background-color: #7dc243; }
     
      #mainNav.local
      {
          background: #3C7987; }
        #mainNav.localnav.nav {
                  background: none; }
        #mainNav.local nav.nav .top {
                          background: #3C7987; }
        #mainNav.local nav.nav a:hover, #mainNav nav.nav a.active {
                        background-color: #7FDCFF; }

In layout page in _Layout file header section:
<header>
        <div class="content-wrapper">
            <div class="float-left">
                <p class="site-title">@Html.ActionLink("your logo here", "Index", "Home")</p>
            </div>
            <div class="float-right">
                <section id="login">
                    @Html.Partial("_LoginPartial")
                </section>
                @{
                    string searchClass = "global";
                    if (HttpContext.Current.Request.Params["searchType"] == "local") 
                    {
                        searchClass = "local"; 
                    }
                }
                <div id="mainNav" class=@searchClass>
                    <nav class="nav" data-nav>
                        <ul>
                          
                            @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "searchForm" }))
                            {
                                @Html.ActionLink("Global","Index","Home", new { searchType= "global" }, null)
                                @Html.ActionLink("Local","Index", "Home", new { searchType = "local" }, null)
                                <input type="search" name="searchMode" value="" placeholder="Search...">
                                <input type="submit" name="submitBtn" value="Search">
                            }
                        </ul>
                    </nav>
                </div>
               <nav>
                    <ul id="menu">
                        <li>@Html.ActionLink("Home", "Index", "Home")</li>
                        <li>@Html.ActionLink("About", "About", "Home")</li>
                        <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                    </ul>
                </nav>
            </div>
        </div>
    </header>
So we are going to toggle class name for navigation bar. If we are in global searchType background color is #7dc243, in local searchType 7FDCFF.

If you are in global searchType your address is :
http://localhost:49853/?searchType=global




In local :

http://localhost:49853/?searchType=local

Thursday, 3 July 2014

Passing object from view to partial view with ViewDataDictionary in ASP.NET MVC

There might be a situation where you have parent model that for instance hold list of objects that you want to pass to partial view. I prefer to not use View Data or ViewBag and make sure that view models I am using always hold information I want to access, but there might be some exceptions.

Take a look at this example
 @Html.Partial("_EditAddress", Model.AddressVM, new ViewDataDictionary { { "statesList", Model.stateList} })
Third parameter in constructor for Partial class is ViewDataDictionary. Using this we can replace ViewData for partial view.
statesList in this example is key and Model.stateList is data that parent Model holds.

To retrieve the data in partial view access it like you would normally access ViewData and cast it to an object you are expecting.

So for instance if we are using dropdownlist helper :
@Html.DropDownListFor(m => m.stateId, new SelectList(ViewData["statesList"] as List<States>, "stateId", "name", "stateId"), "Select state")

Wednesday, 2 July 2014

Get current action and controller name in View in ASP.NET MVC

Being able to get action or controller name on view might come useful when you are using layout for every view(except partial views) in your application.

Layout view
 @if (Request.IsAuthenticated)
                        {
                            <ul>
                                <li>
                                    @Html.ActionLink("Dashboard", "Index", "Home")
                                </li>
                                <li>
                                    @Html.ActionLink("Account", "Manage", "Account")
                                </li>
                                <li>
                                    @Html.ActionLink("GetClients", "Index", "Client")
                                </li>
                                <li>
                                    @Html.ActionLink("Setting", "ListingForOrganisation", "Settings")
                                </li>
                               
                                @if (HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString() == "Client")
                                {
                                <li><span>Add Client</span>
                                    <ul>
                                        <li>
                                            @Html.ActionLink("Temporary client", "AddTemporaryClient", "Client")
                                        </li>
                                        <li>
                                            @Html.ActionLink("Permanent", "AddPermanentClient", "Client")
                                        </li>
                                    </ul>
                                </li>
                                }
                            </ul>
                        }

(HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString() == "Client")
will check if request came from Client controller and if so extra feature will appear.

To get action name replace simply use 
(HttpContext.Current.Request.RequestContext.RouteData.Values["action"].ToString() == "Action name") 



Tuesday, 22 April 2014

Prevent displaying Log In page in partial view on ajax request after session expired in ASP.NET MVC

If you are using partial views with many ajax request, there might be a situation in which user session expires and if you will try to submit form asynchronously via ajax  Log in page will be displayed in partial view(this will occur if you are using authorize attribute on the method you are calling).

Add this method to global.asax.cs file.

 // To use for ajax request when user session expired

protected void Application_EndRequest()
        {
            var context = new HttpContextWrapper(Context);
            // If we're an ajax request, and doing a 302, then we actually need to do a 401
            if (Context.Response.StatusCode == 302 && context.Request.IsAjaxRequest())
            {
                Context.Response.Clear();
                Context.Response.StatusCode = 401;
            }
        }
In main.js file or script section on your main view(cshtml file if using Razor) add this:
var  $document = $(document);

handleAjaxError = function (XMLHttpRequest, textStatus, errorThrown) {
    if (XMLHttpRequest.status == 401) {
        //refresh the page, as we are not longer authorized
        location.reload();
    }
    };
 
Since we already lost session, we want to refresh the page and user will be redirect automatically to Log in page, rather than displaying Log in page in small partial view.

    $document.ajaxComplete(function(event, XMLHttpRequest, AjaxOptions){
        handleAjaxError(XMLHttpRequest);
    }); 

http://api.jquery.com/ajaxComplete/ - More on ajax Complete method

http://stackoverflow.com/questions/3431343/asp-net-mvc-forces-an-ajax-request-be-redirected-to-the-login-page-when-the-form - Inspired by this post

Tuesday, 1 April 2014

Google adsense April Fools joke

After I logged in today to see if google made any April fools joke I discovered one on Ad sense earning page. Regions have been replaced by planets and moons. It might be quite funny at the moment, but in next 30-100 years it might be real.