C# MVC Razor page form

MrM

Joined
Feb 9, 2024
Messages
1
Reaction score
0
Good day,
I have this from in C# MVC
<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" />
}

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 this part:
@Html.DropDownListFor(model => model.ShippingCountryID, new SelectList(Model.ShippingCountries, "ShippingCountryId", "ShippingCountryName"), "Select a Shipping Country", new { @class = "form-control" })

I can select country, and all names are shown, but then I press submit I get error
System.NullReferenceException: 'Object reference not set to an instance of an object.'

Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>.Model.get returned null.

cant figure it out how to solve it, looked at previews examples and it seems like everything should be fine, but still throws the error.

Thaks in advance
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,828
Messages
2,569,736
Members
45,518
Latest member
vlonesshirt

Latest Threads

Top