'the select doesn't post the selected item
On the HttpPost I do not get the selected drop down value.
All other values in the model that is being posted exists.
the CountryList is being posted as null.
The selectedCountryID is the same as initialized in the Get.
The int value stays the same no matter whatever I select in the dropdown.
I don't need the list - just the selected value.
Project is using Core Net 2.2/ MVC
Model
public class GuestRequestModel
{
[Display(Name = "Select Country", Description = "Select Country")]
public int SelectedCountryID { get; set; }
public IEnumerable<SelectListItem> CountryList { get; set; }
}
Controller
[HttpGet]
public IActionResult Create()
{
var listDTO = guestRequestRepository.GetCountryList();
var listForVM = listDTO.Select(t => new SelectListItem
{
Text = t.Description_EN,
Value = t.ID.ToString()
});
model.CountryList = listForVM;
model.SelectedCountryID = country.ID;
return View(model);
}
[HttpPost]
public IActionResult Create(GuestRequestModel model)
{
if (ModelState.IsValid)
{// no selected value here}
}
View
@model GuestStudentWF.Models.GuestRequestModel
<form asp-action="Create">
<div class="form-group">
<label asp-for="SelectedCountryID" class="control-label"></label>
<select asp-for="SelectedCountryID" asp-items="Model.CountryList">
<option>Please select one</option>
</select>
</div>
</form>
I have tried changing the SelectedCountryID to string - it didn't make a diffrence.
Could be that I am missing something fondumental in the MVC-Razor concept
Solution 1:[1]
Same thing happened to me and it was because my select control was not within the form tags.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yusuff Sodiq |
