【问题标题】:How to return text to a span in a .cshtml from a controller in c# asp.net如何从 c# asp.net 中的控制器将文本返回到 .cshtml 中的跨度
【发布时间】:2017-09-01 18:06:22
【问题描述】:

我只是有一个问题,我不知道这是否可能。我一直在四处寻找,没有找到任何东西。我很好奇我的登录控制器中是否有任何内容。如果我的一个 if 语句失败,有什么方法可以将一些文本返回到我的登录视图上的范围?

所以说这在我的登录控制器中的登录方法期间失败了:

if (BadCredentials(checkLogin) == null)
{
     return RedirectToAction("Login", "Login");               
}

有什么方法可以设置为将文本返回到此范围而不是重定向?

<span class="help-block"></span>

【问题讨论】:

    标签: c# html asp.net asp.net-mvc razor


    【解决方案1】:

    在您的控制器中:

    if (BadCredentials(checkLogin) == null)
    {
         ViewBag.SpanText = "This is span text";         
    }
    

    您的看法:

    <span class="help-block">@ViewBag.SpanText</span>
    

    【讨论】:

    • 你们俩都很棒!谢谢您将标记为答案。
    【解决方案2】:

    您可以在ViewBag中设置值并在View中使用它

    在控制器中

    if (BadCredentials(checkLogin) == null)
    {
         ViewBag.YourValue = "some text";
    }
    

    在视图中

    <span class="help-block">@ViewBag.YourValue</span>
    

    【讨论】:

      【解决方案3】:

      您可以使用 Viewbag 属性来显示来自控制器的数据以在 .cshtml 中查看

      在您的控制器中:

      ViewBag.Name = "你的文字";

      在您的 .cshtml 中:

      @ViewBag.Name

      【讨论】:

        【解决方案4】:

        在你的控制器上

        @{
        
            var ImageFile = System.Web.HttpContext.Current.Server.MapPath("~/Document/Picture/" + @Request.Cookies["sTimeStamp"].Value + ".jpg");
            var PanFile = System.Web.HttpContext.Current.Server.MapPath("~/Document/PAN/" + @Request.Cookies["sTimeStamp"].Value + ".jpg");
            var AdhaarFile = System.Web.HttpContext.Current.Server.MapPath("~/Document/Adhaar/" + @Request.Cookies["sTimeStamp"].Value + ".jpg");
        
        }
        @if (System.IO.File.Exists(ImageFile) == false || System.IO.File.Exists(PanFile) == false || System.IO.File.Exists(AdhaarFile) == false)
        {
            ViewBag.KycMessage = "Your KYC has not completed yet. KYC is mandatory to get payment you earned. Following is list of document which is not uploaded.";
        }
        else
        {
            ViewBag.KycMessage = "<span style=\"color:green\">KYC Comlete</span>";
        }
        
        <span>@ViewBag.KycMessage</span>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-08-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-04-09
          • 2023-02-11
          • 2012-03-21
          相关资源
          最近更新 更多