CONNECT WITH US

Featured Post

PartialViewResult in ASP.NET MVC controller

Get article update by Email

return PartialView()… yeah as it suggests it will return partial view but I think many developer does not know about PartialViewResult or have never used it. So let’s have quick discussion on it.

Let start with scratch, as we know @Html.Partial is used to inject partial view markup inside another view and while using @Html.Partial, it will not render layout view simply it will render content of partial view without applying layout view. It is very much necessary because the partial view content will be rendered within another view (or sometime partial view) and hence that view has already applied layout view so there is no need to apply layout view again on partial view. One more important aspect is that generally partial view markup will be reflected as a part of page but not directly as a page (read again!) and layout view contain markup for whole page layout and not for specific part of page. So again no need to apply layout view while rendering partial view.

It is pretty much clear that we can use @Html.Partial for rendering partial view within another view but many a times we need to return partial view from action method. return View() returns ViewResult instance from action method and ViewResult apply layout view. One of the common requirements of returning partial view from action method is that many times we are requesting page markup on demand. For e.g. user clicks on accordion header and it send AJAX request and fetch markup and fill accordion content area. Many times we are fetching popup content with AJAX request and at that time we have to call action method! This is where PartialViewResult (and of course ASP.NET MVC flexibility) comes in picture. In such scenario we can return PartialViewResult from action method by return PartialView() and all is done. PartialViewResult will not apply layout view so we can directly use it to fill page area or popup content or wherever layout markup is not required. I have seen many developers creating blank/empty layout view for same purpose. Following is pseudo code for returning partial view from action method.

if (Request.IsAjaxRequest())
    return PartialView();
else
    return View();

So this is all about partial view in ASP.NET MVC. You can follow me @NandipMakwana for more update on twitter and you can even read ASP.NET MVC 4 article series here.

Read More

ASP.NET 4.5 & MVC 4: Customize oAuth Login UI

Get article update by Email

This post is part of ASP.NET MVC 4 Article Series.

In this quick post, we will see how to customize, oAuth Login UI in ASP.NET 4.5 and MVC 4. For more detail on setting up facebook & twitter login with MVC 4, you can refer my previous post. One of the common requirements with oAuth login is that displaying respective provider’s logo or image.

ASP.NET MVC 4 oAuth Login UI

In ASP.NET 4.5 and MVC 4, we can register oAuth provider in App_Start/AuthConfig.cs file and here we can also pass additional data to oAuth provider if any. We can use this extra data dictionary to pass oAuth provider’s image url to view and based on it we can display image for respective provider.

Register oAuth provider in MVC 4

As we can see in above code snippet that we are passing Icon url with extra data. So we can now access this Icon url from view. Open Views/Account/_ExternalLoginsListPartial.cshtml and replace the classic button markup with below code snippet.

Login UI view

Now run the application by pressing F5 and wow we have brand new login UI for oAuth provider.

Points to keep in mind

  • Make sure you pass extra data for all providers you are registering. Otherwise exception “The given key was not present in the dictionary.” will be thrown when accessing it from view.
  • Carefully notice both code snippets provided above, we are passing image url starting with / but not ~ because in view we are using HTML input type image and not HTML helper so HTML input type image will not understand ~.

You can read more on how to setup facebook and twitter application to use with MVC 4. In next post we will see how to request additional information from oAuth provider.

Read More