I know there are more topics on this subject, but or I have a different problem, or i just do not understand the given solutions. So a revisit for this topic What am I missing here?
this reads the selected value into the selectedview property (and writes it back) So, this works just fine.
VIEW
<div class="form-group">
<div class="col-md-10">
@Html.DropDownListFor(model => model.Gender,
new List<SelectListItem>
{
new SelectListItem { Text = "Male", Value = "m"},
new SelectListItem { Text = "Female", Value = "f"},
}
, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Gender, "", new { @class = "text-danger" })
</div>
</div>
this reads the selected value into the selectedview property (but does NOT read is back)
<div class="form-group">
<div class="col-md-10">
@Html.DropDownListFor(model => model.SchoolID,
@* Model.SchoolList *@
new List<SelectListItem>
{
new SelectListItem { Text = "School 1", Value = "1"},
new SelectListItem { Text = "School 2", Value = "2"},
}
, new { htmlAttributes = new { @class = "form - control" } })
@Html.ValidationMessageFor(model => model.SchoolID, "", new { @class = "text-danger" })
</div>
</div>
model and database setup
MODEL
public string Gender { get; set; }
public string SchoolID { get; set; }
DATABASE TABLE
[Gender] [varchar](50) NULL,
[SchoolID] [int] NOT NULL,
DATABASE STORED PROCEDURE
@Gender varchar(50),
@SchoolID int
Something about model.SchoolID
makes it that the selected value can be set correctly, but it does not return a changed selectedvalue to the model, even though SchoolID
and Gender are both defined as string in the model. So for the DropDownListFor
they both should be of the same type, right? And if so, then I have no clue why one is working and the other is not...
So, some light on this issue will be much appreciated.
I had the next line of code in the view:
@Html.HiddenFor(model => model.SchoolID) ...
This disabled the update of the selectedvalue for dropdownlistfor (or to be more precise probably disabled the update of model.SchoolID by not posting the value?) Whatever the reason, removing this line solved the problem.
Thank you @Stephen, for your support, and thank you @Chris, for pointing me into the right direction.