I have class like
public class MyClass
{
public object Jobj{get; set;}
public string summary{get; set;}
public string someMethod()
{
//Do some work
return "";
}
}
and a post action creating instance of this class and initialising properties with form data inputs
[HttpPost]
public dynamic controllerAction()
{
try
{
var instance= new MyClass
{
Jobj= JObject.Parse(HttpContext.Current.Request.Params["formInputKey1"]),
summary= HttpContext.Current.Request.Params["summaryKey"]
};
return instance.SomeMethod();
}
catch (Exception ex)
{
return ex;
}
}
I wanted to know how to enforce null property check for form-data input
What I tried:
public class MyClass
{
[Required]
public object Jobj{get; set;}
[Required(AllowEmptyStrings = false), StringLength(maximumLength: 100, MinimumLength = 1)]
public string summary{get; set;}
public string someMethod()
{
//Do some work
return "";
}
}
and a post action creating instance of this class and initialising properties with form data inputs
[HttpPost]
public dynamic controllerAction()
{
try
{
var instance= new MyClass
{
Jobj= JObject.Parse(HttpContext.Current.Request.Params["formInputKey1"]),
summary= HttpContext.Current.Request.Params["summaryKey"]
};
var isModelStateValid = ModelState.IsValid;
if(!isModelStateValid )
{
throw ArgumentException("Found Data Null");
}
return instance.SomeMethod();
}
catch (Exception ex)
{
return ex;
}
}
But ModelState.IsValid
is always true as it was containing count = 0