【问题标题】:Check whether an element exists in mvc formcollection检查mvc formcollection中是否存在元素
【发布时间】:2015-02-02 00:07:36
【问题描述】:

我在 mvc 控制器中以 FormCollection 的形式接收一些数据。我想检查表单集合中是否存在特定键。

 public JsonResult FullRetailerUpdate(FormCollection data)
 {
     //I want to check if 
     //data["AnElement"] is exist
 }

请帮忙。

【问题讨论】:

    标签: c# asp.net-mvc formcollection


    【解决方案1】:

    尝试使用.Contains():-

     public JsonResult FullRetailerUpdate(FormCollection data)
     {
        if (data.AllKeys.Contains("AnElement")) 
        {
          // Your Stuff
        }
        else
        {
          // Your Stuff
        }   
     }
    

    【讨论】:

      【解决方案2】:

      我知道问题是关于 FormCollection 但对于那些使用 IFormCollection 的人来说,这里是解决方案。

      public IActionResult GetProjectDelivery(IFormCollection data)
      {
          if (data.ContainsKey("AnElement"))
          {
              // do stuff
          }
          else
          {
              // do stuff
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-20
        • 1970-01-01
        • 1970-01-01
        • 2011-09-26
        相关资源
        最近更新 更多