MVC HTMLHelper to know a mobile device
CSS is really powerful and you can do anything really with it a website, but sometime simplicity is the better way.
When developing a site now, you always need to think of mobile devices and sometime the layout needs a whole different layout between mobile and desktop.

The simplest and easiest way I found was using a HTML Helper to find out what type of device or even browser is viewing my site and then using this knowledge to drive the partial view paths.

I would have parietal view for a layout for desktop and another for mobile. I know duplication is not good, but you can use partial view to reduce the duplication and have the main logic within the root views. Then have different views for just the layout

[HEADER]HTML Helper[/HEADER]


public static class BrowserQueryHTMLHelper
{

public static string CustomLayoutViewPartialViewURL(this HtmlHelper html)
{
var browser = html.ViewContext.HttpContext.Request.Browser;

if(browser.IsMobileDevice)
{
return "MobileLayoutView";
}else

{
return "DesktopLayoutView";
}
}
}




[HEADER]HTML Razor side[/HEADER]

@using MVCPortal.HtmlHelpers

@{Html.RenderPartial(Html.CustomLayoutViewPartialViewURL(), Model);}
Created: 10/04/2017 Total Comment: 0

Comments

(Not Displayed)
Human validation
Enter 7242 number, before submitting to confirm your human
[Home] [All Blogs] [Contact] [Me]