the error states that the Model itself is null. be sure the action created the model , initializes ShippingCountries property and passes the model to the view.
C# MVC DropDownList
Good day, I have this form
@using ECommerceNet7MVC.Models.ShippingCustomerModels;
@model CustomerShippingAddress
<div class="container">
@using (Html.BeginForm("Index", "CustomerOrder", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<br />
<div class="col-12 row">
<div class="col-12">
Enter Shipping Address
</div>
<div class="row">
<div class="col-3">
<label>Enter Customer Name</label>
</div>
<div class="col-6">
<input asp-for="FirstName" class="form-control" />
<span asp-validation-for="FirstName" class="text-danger"></span>
</div>
</div>
<div class="row">
<div class="col-3">
<label>Enter Customer Lastname</label>
</div>
<div class="col-6">
<input asp-for="LastName" class="form-control" />
<span asp-validation-for="LastName" class="text-danger"></span>
</div>
</div>
<div class="row">
<div class="col-3">
<label>Shipping Country</label>
</div>
<div class="col-6">
@Html.DropDownListFor(model => model.ShippingCountryID, new SelectList(Model.ShippingCountries, "ShippingCountryId", "ShippingCountryName"), "Select a Shipping Country", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ShippingCountryID,
"", new {@class = "text-danger"})
</div>
</div>
<div class="row">
<div class="col-3">
<label>State</label>
</div>
<div class="col-6">
<input asp-for="ShippingState" class="form-control">
<span asp-validation-for="ShippingState" class="text-danger"></span>
</div>
</div>
<div class="row">
<div class="col-3">
<label>City</label>
</div>
<div class="col-6">
<input asp-for="ShippingCity" class="form-control">
<span asp-validation-for="ShippingCity" class="text-danger"></span>
</div>
</div>
<div class="row">
<div class="col-3">
<label>House Number</label>
</div>
<div class="col-6">
<input asp-for="HouseNumber" class="form-control">
<span asp-validation-for="HouseNumber" class="text-danger"></span>
</div>
</div>
<div class="row">
<div class="col-3">
<label>Appartment Number</label>
</div>
<div class="col-6">
<input asp-for="AppartmentNumber" class="form-control">
<span asp-validation-for="AppartmentNumber" class="text-danger"></span>
</div>
</div>
<div class="row">
<div class="col-3">
<label>Postal Code</label>
</div>
<div class="col-6">
<input asp-for="ShippingPostalCode" class="form-control">
<span asp-validation-for="ShippingPostalCode" class="text-danger"></span>
</div>
</div>
<button type="submit" class="btn btn-success">Submit</button>
</div>
}
</div>
@section Scripts {
<partial name="_ValidationScriptsPartial" />
}
I have this class
public class CustomerShippingAddress
{
[Required]
public string FirstName { get; set; } = string.Empty;
[Required]
public string LastName { get; set; } = string.Empty;
[Required]
public int? ShippingCountryID { get; set; }
[Required]
public string ShippingState { get; set; } = string.Empty;
[Required]
public string ShippingCity { get; set; } = string.Empty;
[Required]
public int HouseNumber { get; set; }
[ValidateNever]
public int? AppartmentNumber { get; set; }
[Required]
public string ShippingPostalCode { get; set; } = string.Empty;
[ValidateNever]
public List<ShippingCountryCustomer> ShippingCountries { get; set; }
}
I get error on
@Html.DropDownListFor(model => model.ShippingCountryID, new SelectList(Model.ShippingCountries, "ShippingCountryId", "ShippingCountryName"), "Select a Shipping Country", new { @class = "form-control" })
i throws System.NullReferenceException: 'Object reference not set to an instance of an object.' Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>.Model.get returned null.
what object is not set? I pass everything fully initialized and i can select name of country, but it throw error.
All the help is appriciated
3 answers
Sort by: Most helpful
-
-
Lan Huang-MSFT 29,591 Reputation points Microsoft Vendor
2024-02-12T03:23:06.3766667+00:00 Hi @Modestas Vačerskas,
what object is not set? I pass everything fully initialized and i can select name of country, but it throw error.
From your description you can already select the CountryName, have you successfully populated the DropDownList? When exactly did you report this bug? After clicking the submit button?
You have some information that was not provided, such as the ShippingCountryCustomer class and how to populate the CustomerShippingAddress.
Based on your code, I tested it using test data and it worked successfully. Maybe you can compare it to your own code and see where the problem lies.
public IActionResult Index() { CustomerShippingAddress customerShippingAddress = new CustomerShippingAddress(); List<ShippingCountryCustomer> shippingCountryCustomers = new List<ShippingCountryCustomer> { new ShippingCountryCustomer { ShippingCountryId = 1, ShippingCountryName = "A" }, new ShippingCountryCustomer { ShippingCountryId = 2, ShippingCountryName = "B" } }; customerShippingAddress.ShippingCountryID = 1; customerShippingAddress.ShippingCountries = shippingCountryCustomers; return View(customerShippingAddress); } [HttpPost] public IActionResult Index(CustomerShippingAddress customerShippingAddress) { return View(customerShippingAddress); }
public class CustomerShippingAddress { [Required] public string FirstName { get; set; } = string.Empty; [Required] public string LastName { get; set; } = string.Empty; [Required] public int? ShippingCountryID { get; set; } [Required] public string ShippingState { get; set; } = string.Empty; [Required] public string ShippingCity { get; set; } = string.Empty; [Required] public int HouseNumber { get; set; } [ValidateNever] public int? AppartmentNumber { get; set; } [Required] public string ShippingPostalCode { get; set; } = string.Empty; [ValidateNever] public List<ShippingCountryCustomer> ShippingCountries { get; set; } } public class ShippingCountryCustomer { public int ShippingCountryId { get; set; } public string ShippingCountryName { get; set; } }
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. -
Modestas Vačerskas 0 Reputation points
2024-02-12T08:20:27.15+00:00 Thanks fixed it, it was simply moving to next view, and throwing error because that view was required new object. I set breakpoint and it worked. Thanks for your time.