Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在MVC.cshtml页面中使用Javascript if-else_Javascript_C#_Asp.net Mvc_Razor - Fatal编程技术网

在MVC.cshtml页面中使用Javascript if-else

在MVC.cshtml页面中使用Javascript if-else,javascript,c#,asp.net-mvc,razor,Javascript,C#,Asp.net Mvc,Razor,MVC cshtml页面中是否有使用if-else语句的方法 <div class="primary-content"> if(DetectIE()) { <embed data-bind="attr: { src: data.files()[currentPage()] + '#toolbar=0&amp;navpanes=0&amp;scrollbar=0' }" type="appl

MVC cshtml页面中是否有使用if-else语句的方法

 <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>

if(DetectIE())
{
}
其他的
{
}
我有一个javascript代码来检测当前浏览器是否为Internet explorer。如果是IE,则使用
标记,否则使用
标记

如有任何建议或帮助,将不胜感激


提前感谢

您不能直接在razor中使用javascript函数。因此最好使用Request.Browser来获取Browsername

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

}
@if (IsIE)
{
    // Your Razor here
}
else
{
    //  Your Razor here
}
由于
DetectIE()
是一个JS函数,因此不能将其与Razor
@if
块进行比较。您应该使用
jQuery.append()
将其放入
中,以便将适当的标记附加到目标元素中:

<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>
<div id="targetElement" class="primary-content"></div>
然后将该值传递给
ViewBag

ViewBag.IsIE = isIE;
JS使用示例

if (@ViewBag.IsIE)
{
   // render embed tag
}
else
{
   // render object tag
}

只需使用
@if(DetectIE()){//embed tag}else{//object tag}
语句。但是什么是
DetectIE()
do?DetectIE它是一种检测互联网的javascript方法explorer@if将使用c#if-else而不是javascript如果elsepoe,则不能将JS方法与Razor
if
语句一起使用。将if语句放在
标记内,并使用类似
$.append(“…”)
的内容追加元素。使用
Request.Browser
来确定控制器中的浏览器可能更容易,然后只需向视图传递一个
bool
属性,指示其是否为IE(作为视图模型属性或
ViewBag
属性)我的方法是javascript方法
if (@ViewBag.IsIE)
{
   // render embed tag
}
else
{
   // render object tag
}