1

Is there a way to use if else statment inside MVC cshtml page

 <div class="primary-content">
            if(DetectIE())
            {
            <embed data-bind="attr: { src: data.files()[currentPage()] + '#toolbar=0&amp;navpanes=0&amp;scrollbar=0' }" type="application/pdf" style="width: 100%; height: 800px !important;">
            }
            else
            {
            <object data-bind="attr: { data: data.files()[currentPage()] + '#toolbar=0&amp;navpanes=0&amp;scrollbar=0' }" type="application/pdf" width="100%" height="600px"></object>
            }
        </div>

I have a javascript code to detect if the current browser is Internet explorer or not . If it is a IE then <embed> tag is used otherwise <object> tag is used.

Any suggestions or help would be appreciated.

Thanks in advance

6
  • Just use @if (DetectIE()) { // embed tag } else { // object tag } statement. But what is DetectIE() do? Commented Nov 1, 2018 at 4:18
  • DetectIEit is a javascript method to detect internet explorer Commented Nov 1, 2018 at 4:19
  • @if will use c# if else not javascript if else Commented Nov 1, 2018 at 4:20
  • Nope, you cannot use JS method with Razor if statement. Place the if statement inside the <script> tag and append the element with something like $.append('<embed>...</embed>'). Commented Nov 1, 2018 at 4:22
  • It might be easier to use Request.Browser to determine the browser in the controller, and then just pass a bool property to your view indicating if its IE or not (as a view model property or a ViewBag property) Commented Nov 1, 2018 at 4:24

2 Answers 2

1

Since DetectIE() is a JS function, you can't use it as comparison to Razor @if block. You should put it inside <script> with jQuery.append() to append the proper tag into target element:

<script>
$(function() {
    // other stuff

    if (DetectIE())
    {
        $('#targetElement').append("<embed data-bind=\"attr: { src: data.files()[currentPage()] + '#toolbar=0&amp;navpanes=0&amp;scrollbar=0' }\" type=\"application/pdf\" style=\"width: 100%; height: 800px !important;\">");
    }
    else
    {
        $('#targetElement').append("<object data-bind=\"attr: { data: data.files()[currentPage()] + '#toolbar=0&amp;navpanes=0&amp;scrollbar=0' }\" type=\"application/pdf\" width=\"100%\" height=\"600px\"></object>");
    }
});
</script>

Example of the target element:

<div id="targetElement" class="primary-content"></div>

If you want to check any version of IE from controller side, you can use this:

bool isIE = (Request.Browser.Browser == "IE" || Request.Browser.Browser == "InternetExplorer");

And then pass the value to ViewBag:

ViewBag.IsIE = isIE;

JS usage example

if (@ViewBag.IsIE)
{
   // render embed tag
}
else
{
   // render object tag
}
Sign up to request clarification or add additional context in comments.

Comments

0

You cannot use javascript function inside razor directly..so bettter use Request .Browser to Get the Browsername

@{bool IsIE = false ;
if (Request.Browser.Type.ToUpper().Contains("IE"))
{ 
    IsIE = true;
}

}
@if (IsIE)
{
    // Your Razor here
}
else
{
    //  Your Razor here
}

1 Comment

My method is a javascript method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.