Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether an element exists in mvc formcollection

I am receiving some data in mvc controller as FormCollection. I would like to check if there is a particular key exists in the formcollection.

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

Please help.

like image 423
s.k.paul Avatar asked Dec 04 '14 07:12

s.k.paul


2 Answers

Try using .Contains():-

 public JsonResult FullRetailerUpdate(FormCollection data)
 {
    if (data.AllKeys.Contains("AnElement")) 
    {
      // Your Stuff
    }
    else
    {
      // Your Stuff
    }   
 }
like image 82
Kartikeya Khosla Avatar answered Sep 23 '22 08:09

Kartikeya Khosla


I know that the question was about FormCollection but for those using IFormCollection here is the solution.

public IActionResult GetProjectDelivery(IFormCollection data)
{
    if (data.ContainsKey("AnElement"))
    {
        // do stuff
    }
    else
    {
        // do stuff
    }
}
like image 42
Darren Avatar answered Sep 25 '22 08:09

Darren