Hi guys
I have an action method index that creates an instance for an object from another class that has properties and methods that return classes (I hope I am using correct terms!) *Beginner Alert*
namespace ErrorRedirect.Controllers
{
public class HomeController : Controller
{
private readonly ILogger _logger;
public HomeController(ILogger logger)
{
_logger = logger;
}
public IActionResult Index()
{
//string winuser = User.Identity.Name.Split('\\')[1];
string winuser = "rajesh";
KefsUser kuser = null;
string fName = string.Empty;
kuser = new KefsUser().GetKefsUser(winuser);
if (kuser != null)
{
fName = kuser.empFullName;
return View(kuser);
}
else
{
return View("ShowError", winuser + " is an invalid KEFS Username");
}
}
and the referenced class is like this.
using ErrorRedirect.Utility;
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
namespace ErrorRedirect.Models
{
public class KefsUser
{
string connectionString = ConnectionString.CName;
public string employeeCode { get; set; }
public int empUserLevel { get; set; }
public string empFullName { get; set; }
public string empEmail { get; set; }
public KefsUser GetKefsUser(string winuser)
{
KefsUser kuser = new KefsUser();
string Orasql = @"Select * from
KAZ_KEFS_LOGIN_V
WHERE LOGIN_NAME='" + winuser + "'";
OracleConnection conn = new OracleConnection(connectionString);
OracleCommand cmd = new OracleCommand(Orasql, conn);
//OracleDataReader dr = new OracleDataReader();
cmd.CommandType = CommandType.Text;
OracleDataAdapter da = new OracleDataAdapter(cmd);
DataTable dt = new DataTable();
conn.Open();
da.Fill(dt);
conn.Close();
int? reccount = dt.Rows.Count;
if (reccount != 0)
{
foreach (DataRow dr in dt.Rows)
{
kuser.employeeCode = Convert.ToString(dr["EMP_CODE"]);
kuser.empUserLevel = Convert.ToInt32(dr["USER_LEVEL"]);
kuser.empFullName = Convert.ToString(dr["FULL_NAME"]);
kuser.empEmail = Convert.ToString(dr["EMAIL_ADDRESS"]);
}
return kuser;
}
else
{
return null;
}
}
}
I'm deliberately passing a username that does not exist in the application user table & the method returns an empty class to my action method call. Now I am checking whether the instance of the object is null & manage the routing of the application (redirects to a page that shows error message)
Basically I am curious to know whether I can use Modelstate.IsValid to validate the object? Please comment.

Mohamed Azarudeen ZPosted Jun 13, 2023, 6:43 AM
The `ModelState.IsValid` property is typically used to validate the data submitted by a user in a form and check if it meets the validation rules specified in the model. It is not designed to validate objects retrieved from a database or instantiated within the code.
In your scenario, you are retrieving data from the database and populating the properties of the `KefsUser` object based on the result. Since you are not directly accepting user input in this process, there is no need to use `ModelState.IsValid` to validate the object.
Instead, you can perform custom validation checks on the retrieved object, such as checking if the object is null or if any required properties have valid values. Based on your code, you are already checking if the `kuser` object is null and handling it accordingly.
If you have specific validation requirements for the `KefsUser` object, you can consider adding validation attributes to the class properties, such as `[Required]`, `[StringLength]`, or custom validation attributes. These attributes can be useful when you perform input validation before saving the data back to the database or when using model binding with user input.
In summary, while `ModelState.IsValid` is not applicable in your current scenario, you can implement custom validation checks on the retrieved `KefsUser` object as needed.
Rajesh ThampiPosted Jun 13, 2023, 7:05 AM
Thank you very much Azar!
Actually, I'm kind of getting confused being a beginner for "Nth" time :)). Being an Oracle developer, my involvement with .Net development is pretty limited and whenever I came back, I find new terms that confuse me beyond limits.
Thanks for your time and detailed explanations. Appreciate it!