【问题标题】:how to check the value received from controller is not null in view in asp.net mvc?如何在asp.net mvc的视图中检查从控制器接收到的值不为空?
【发布时间】:2020-01-15 05:29:42
【问题描述】:

这是我的控制器

public ActionResult Index()
    {
        return View();
    }
    [HttpPost]
    public ActionResult Validate(HttpPostedFileBase excelfile)
    {
        if(excelfile==null || excelfile.ContentLength==0)
        {
            TempData["Message"] = "Please select a excel file";
            return View("Index");

       }

从这里我将TempData["Message"] 传递给view。如何检查TempData["Message"] 是否在视图中不为空?

在我看来,我正在使用<script> @TempData["Message"]</script> 发出警报

【问题讨论】:

标签: asp.net asp.net-mvc razor


【解决方案1】:

你可以使用:

@if (TempData["Message"] != null)
    {
      @TempData["Message"].ToString()
   }

【讨论】:

    【解决方案2】:

    试试这个,它会解决你的问题

    $(document).ready(function() {
    var success = @((TempData["Message"] != null).ToString().ToLower());
    
    if (success == true) {
            //Do your code here
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-15
      • 2021-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多