Rajesh Thampi

Rajesh Thampi

  • NA
  • 128
  • 5.3k

How to deal with an empty model returned?

Jun 13 2023 5:44 AM

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<HomeController> _logger;

        public HomeController(ILogger<HomeController> 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.


Answers (2)