System.NullReferenceException: 'Object reference not set to an instance of an object.' model was null

Naveinthiran 20 Reputation points
2024-06-21T09:37:52.91+00:00

sdafdasf

model

{

public int VehicleID { get; set; }

public int VehicleTypeID { get; set; }

public string Brand { get; set; }

public string Model { get; set; }

public int Capacity { get; set; } = 0;

public bool IsActive { get; set; }

}

controller

// GET: Vehicle

[AccessRights(AccessKey = "VE")]

public ActionResult Vehicle()

{

    DataSet tempDS = new DataSet();

    string __ConnectionString = ConfigurationManager.ConnectionStrings["MySqlConnector"].ConnectionString;

    tempDS = SelectDataSet(__ConnectionString, "SELECT * FROM vehicle WHERE IsActive = 1");

    var ds = tempDS;

    ViewBag.dataSource = ds.Tables[0];

    ViewBag.Search = true;

    return View();

}

[AccessRights(AccessKey = "VE")]

public ActionResult VehicleInsert(VehicleModel model)

{

    try

    {

        string baseSql = "INSERT INTO `vehicle` (`Brand`, `Model`, `IsActive`) VALUES (@brand, @model, @isActive);";

        Dictionary<string, object> param = new Dictionary<string, object>

        {

            { "@brand", model.Brand },

            { "@model", model.Model },

            { "@isActive", model.IsActive }

        };

        DB2.ExecuteNonQueryEscape(baseSql, param);

    }

    catch (Exception ex)

    {

        return Json(new { success = false, error = ex.Message });

    }

    return Json(new { success = true });

}

[HttpGet]

[AccessRights(AccessKey = "VE")]

public ActionResult EditVehicle(int ID)

{

    DataSet tempDS = new DataSet();

    string __ConnectionString = ConfigurationManager.ConnectionStrings["MySqlConnector"].ConnectionString;

    string query = $"SELECT * FROM vehicle WHERE VehicleID=" + ID + ";";

    tempDS = SelectDataSet(__ConnectionString, query);

    var ds = tempDS;

    VehicleModel model = new VehicleModel();

    foreach (DataRow row in ds.Tables[0].Rows)

    {

        model = (DBObjectMapper.Map<VehicleModel>(row));

    }

    return PartialView(model);

}

[AccessRights(AccessKey = "VE")]

public ActionResult VehicleUpdate(VehicleModel model)

{

    try

    {

        if (model.VehicleID != 0)

        {

            string baseSql = "UPDATE `vehicle` SET `Brand` = @brand, `Model` = @model, `IsActive` = @isActive WHERE `VehicleID` = @vehicleId";

            Dictionary<string, object> param = new Dictionary<string, object>

            {

                { "@brand", model.Brand },

                { "@model", model.Model },

                { "@isActive", model.IsActive }

            };

            DB2.ExecuteNonQueryEscape(baseSql, param);

        }

        else

        {

            return Json(new { success = false, error = "ID cannot be null" });

        }

    }

    catch (Exception ex)

    {

        return Json(new { success = false, error = ex.Message });

    }

    return Json(new { success = true });

}

[AccessRights(AccessKey = "VE")]

public ActionResult VehicleDelete(VehicleModel model)

{

    try

    {

        if (model.VehicleID != 0)

        {

            string baseSql = "UPDATE `vehicle` SET `IsActive` = @isActive WHERE `VehicleID` = @vehicleId;";

            Dictionary<string, object> param = new Dictionary<string, object>()

            {

                { "@vehicleId", model.VehicleID },

                { "@isActive", 0 },

            };

            DB2.ExecuteQueryScalarEscape(baseSql, param);

        }

        else

        {

            return Json(new { success = false, error = "ID cannot be null" });

        }

    }

    catch (Exception ex)

    {

        return Json(new { success = false, error = ex.Message });

    }

    return Json(new { success = true });

}
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,562 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,325 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,813 questions
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
918 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,554 questions
{count} votes

Accepted answer
  1. AgaveJoe 27,256 Reputation points
    2024-06-21T18:30:38.4866667+00:00

    Change the "model" to something else like "vm". The name "model" is causing a naming conflict with the property named "model"

    public ActionResult VehicleInsert(VehicleModel vm)
    {
    
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. SurferOnWww 2,326 Reputation points
    2024-06-21T11:00:49.32+00:00

    Use Fiddler to see if the data are properly sent from client to server.

    Use debugger of Visual Studio to see if the data sent from client are properly bound to the model of the action method VehicleInsert(VehicleModel model).

    1 person found this answer helpful.

  2. Soaad Nahas 0 Reputation points
    2024-06-21T17:47:56.88+00:00

    شكرا للتواصل

    0 comments No comments