I have a controller action where I validate if any value is entered in fields or not. Here is how I do it:
[HttpPost]
public ActionResult ValidateFields(string Desc, string Status, string Name )
{
string[] fields = new string[3];
if (string.IsNullOrEmpty(Desc))
fields[0] = "#Desc";
if (string.IsNullOrEmpty(Status))
fields[1] = "#Status";
if (string.IsNullOrEmpty(Name))
fields[2] = "#Name ";
// Check if the initialized array "fields" has any items in it.
if (fields != null)
{ return content("Please enter valid values for " + fields); }
return content("Validation Successful");
}
Here array "fields" is initialized and hence its length is never 0. Also, checking for null does not work. All I can do i loop through the array and check if it has any items in it.
Is there a better way of checking if an array has any items in it or just null values?
Also, if there is any better way of validating fields than how I am doing, please let me know. I want it to be maintainable, if tomorrow I add new fields I want to spend as little time I can to validating them.