MVC DropdownlistFor not reverting to default value on null
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a page which sets the controls on another page to required (-1), disabled (0) or optional (null) from a db table.
They all use the same Viewbag param as the source.
here is the dropdown code which is repeated x number of times.
@Html.DropDownListFor(x=>x.Control1, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
@Html.DropDownListFor(x=>x.Control2, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
@Html.DropDownListFor(x=>x.Control3, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
@Html.DropDownListFor(x=>x.Control4, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
@Html.DropDownListFor(x=>x.Control5, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
StateList Contains
List<SelectListItem> states = new List<SelectListItem>();
states.Add(new SelectListItem { Text = "Required", Value = "-1" });
states.Add(new SelectListItem { Text = "Disabled", Value = "0" });
ViewBag.StateList = states;
This loads up correctly if they are all null, they all display optional.
if i set each one to either required or disabled it works correctly as well.
if i leave the first couple to optional and set number 3 to required and leave the rest it saves correctly in the db as
null,null,-1,null,null,etc
but when it displays the first two are optional but all others are set to whatever i set in control 3 be it required or disabled.
So what should display as
Optional,Optional,Required,Optional,Optional etc
It displays as
Optional,Optional,Required,Required,Required.
Any ideas?
**** EDIT ***
Ok Definitly a binding issue to the Dropdownlistfor...
i replaced them with
<select id="Control1" name="Control1" class="form-control">
@foreach (SelectListItem option in ViewBag.StateList)
{
if (option.Value == Model.Control1.ToString())
{
<option value="@option.Value" selected='selected'>@option.Text</option>
}
else
{
<option value="@option.Value">@option.Text</option>
}
}
</select>
and it works as expected....
this unfortunately turns one line of code into 13.... which i'm not particularly happy about.... so hopefully this info will help in finding a solution using the intended use of dropdownlist for....
c# asp.net-mvc razor html.dropdownlistfor
|
show 1 more comment
I have a page which sets the controls on another page to required (-1), disabled (0) or optional (null) from a db table.
They all use the same Viewbag param as the source.
here is the dropdown code which is repeated x number of times.
@Html.DropDownListFor(x=>x.Control1, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
@Html.DropDownListFor(x=>x.Control2, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
@Html.DropDownListFor(x=>x.Control3, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
@Html.DropDownListFor(x=>x.Control4, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
@Html.DropDownListFor(x=>x.Control5, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
StateList Contains
List<SelectListItem> states = new List<SelectListItem>();
states.Add(new SelectListItem { Text = "Required", Value = "-1" });
states.Add(new SelectListItem { Text = "Disabled", Value = "0" });
ViewBag.StateList = states;
This loads up correctly if they are all null, they all display optional.
if i set each one to either required or disabled it works correctly as well.
if i leave the first couple to optional and set number 3 to required and leave the rest it saves correctly in the db as
null,null,-1,null,null,etc
but when it displays the first two are optional but all others are set to whatever i set in control 3 be it required or disabled.
So what should display as
Optional,Optional,Required,Optional,Optional etc
It displays as
Optional,Optional,Required,Required,Required.
Any ideas?
**** EDIT ***
Ok Definitly a binding issue to the Dropdownlistfor...
i replaced them with
<select id="Control1" name="Control1" class="form-control">
@foreach (SelectListItem option in ViewBag.StateList)
{
if (option.Value == Model.Control1.ToString())
{
<option value="@option.Value" selected='selected'>@option.Text</option>
}
else
{
<option value="@option.Value">@option.Text</option>
}
}
</select>
and it works as expected....
this unfortunately turns one line of code into 13.... which i'm not particularly happy about.... so hopefully this info will help in finding a solution using the intended use of dropdownlist for....
c# asp.net-mvc razor html.dropdownlistfor
Your 3rd and 4th DropDown, what does the@Html.DropDownListFor
look like?
– Izzy
Jan 3 at 11:07
exactly the same just with a different prop binding eg@Html.DropDownListFor(x=>x.Control3, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
,@Html.DropDownListFor(x=>x.Control4, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
– L Riley
Jan 3 at 11:33
If you put a breakpoint, doControl4
andControl5
have the values you expect?
– Dan Dumitru
Jan 3 at 11:40
When the record comes from the db 4 & 5 have the values i'm expecting so its definitely not a source data issue but a display or binding problem. They are being passed the correct values (null) but are displaying the first non null value that's displayed.
– L Riley
Jan 3 at 11:49
@LRiley Make yourControl4
&Control5
propsnullable
– Izzy
Jan 3 at 11:51
|
show 1 more comment
I have a page which sets the controls on another page to required (-1), disabled (0) or optional (null) from a db table.
They all use the same Viewbag param as the source.
here is the dropdown code which is repeated x number of times.
@Html.DropDownListFor(x=>x.Control1, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
@Html.DropDownListFor(x=>x.Control2, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
@Html.DropDownListFor(x=>x.Control3, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
@Html.DropDownListFor(x=>x.Control4, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
@Html.DropDownListFor(x=>x.Control5, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
StateList Contains
List<SelectListItem> states = new List<SelectListItem>();
states.Add(new SelectListItem { Text = "Required", Value = "-1" });
states.Add(new SelectListItem { Text = "Disabled", Value = "0" });
ViewBag.StateList = states;
This loads up correctly if they are all null, they all display optional.
if i set each one to either required or disabled it works correctly as well.
if i leave the first couple to optional and set number 3 to required and leave the rest it saves correctly in the db as
null,null,-1,null,null,etc
but when it displays the first two are optional but all others are set to whatever i set in control 3 be it required or disabled.
So what should display as
Optional,Optional,Required,Optional,Optional etc
It displays as
Optional,Optional,Required,Required,Required.
Any ideas?
**** EDIT ***
Ok Definitly a binding issue to the Dropdownlistfor...
i replaced them with
<select id="Control1" name="Control1" class="form-control">
@foreach (SelectListItem option in ViewBag.StateList)
{
if (option.Value == Model.Control1.ToString())
{
<option value="@option.Value" selected='selected'>@option.Text</option>
}
else
{
<option value="@option.Value">@option.Text</option>
}
}
</select>
and it works as expected....
this unfortunately turns one line of code into 13.... which i'm not particularly happy about.... so hopefully this info will help in finding a solution using the intended use of dropdownlist for....
c# asp.net-mvc razor html.dropdownlistfor
I have a page which sets the controls on another page to required (-1), disabled (0) or optional (null) from a db table.
They all use the same Viewbag param as the source.
here is the dropdown code which is repeated x number of times.
@Html.DropDownListFor(x=>x.Control1, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
@Html.DropDownListFor(x=>x.Control2, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
@Html.DropDownListFor(x=>x.Control3, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
@Html.DropDownListFor(x=>x.Control4, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
@Html.DropDownListFor(x=>x.Control5, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
StateList Contains
List<SelectListItem> states = new List<SelectListItem>();
states.Add(new SelectListItem { Text = "Required", Value = "-1" });
states.Add(new SelectListItem { Text = "Disabled", Value = "0" });
ViewBag.StateList = states;
This loads up correctly if they are all null, they all display optional.
if i set each one to either required or disabled it works correctly as well.
if i leave the first couple to optional and set number 3 to required and leave the rest it saves correctly in the db as
null,null,-1,null,null,etc
but when it displays the first two are optional but all others are set to whatever i set in control 3 be it required or disabled.
So what should display as
Optional,Optional,Required,Optional,Optional etc
It displays as
Optional,Optional,Required,Required,Required.
Any ideas?
**** EDIT ***
Ok Definitly a binding issue to the Dropdownlistfor...
i replaced them with
<select id="Control1" name="Control1" class="form-control">
@foreach (SelectListItem option in ViewBag.StateList)
{
if (option.Value == Model.Control1.ToString())
{
<option value="@option.Value" selected='selected'>@option.Text</option>
}
else
{
<option value="@option.Value">@option.Text</option>
}
}
</select>
and it works as expected....
this unfortunately turns one line of code into 13.... which i'm not particularly happy about.... so hopefully this info will help in finding a solution using the intended use of dropdownlist for....
c# asp.net-mvc razor html.dropdownlistfor
c# asp.net-mvc razor html.dropdownlistfor
edited Jan 4 at 10:00
L Riley
asked Jan 3 at 10:59
L RileyL Riley
124113
124113
Your 3rd and 4th DropDown, what does the@Html.DropDownListFor
look like?
– Izzy
Jan 3 at 11:07
exactly the same just with a different prop binding eg@Html.DropDownListFor(x=>x.Control3, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
,@Html.DropDownListFor(x=>x.Control4, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
– L Riley
Jan 3 at 11:33
If you put a breakpoint, doControl4
andControl5
have the values you expect?
– Dan Dumitru
Jan 3 at 11:40
When the record comes from the db 4 & 5 have the values i'm expecting so its definitely not a source data issue but a display or binding problem. They are being passed the correct values (null) but are displaying the first non null value that's displayed.
– L Riley
Jan 3 at 11:49
@LRiley Make yourControl4
&Control5
propsnullable
– Izzy
Jan 3 at 11:51
|
show 1 more comment
Your 3rd and 4th DropDown, what does the@Html.DropDownListFor
look like?
– Izzy
Jan 3 at 11:07
exactly the same just with a different prop binding eg@Html.DropDownListFor(x=>x.Control3, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
,@Html.DropDownListFor(x=>x.Control4, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
– L Riley
Jan 3 at 11:33
If you put a breakpoint, doControl4
andControl5
have the values you expect?
– Dan Dumitru
Jan 3 at 11:40
When the record comes from the db 4 & 5 have the values i'm expecting so its definitely not a source data issue but a display or binding problem. They are being passed the correct values (null) but are displaying the first non null value that's displayed.
– L Riley
Jan 3 at 11:49
@LRiley Make yourControl4
&Control5
propsnullable
– Izzy
Jan 3 at 11:51
Your 3rd and 4th DropDown, what does the
@Html.DropDownListFor
look like?– Izzy
Jan 3 at 11:07
Your 3rd and 4th DropDown, what does the
@Html.DropDownListFor
look like?– Izzy
Jan 3 at 11:07
exactly the same just with a different prop binding eg
@Html.DropDownListFor(x=>x.Control3, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
, @Html.DropDownListFor(x=>x.Control4, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
– L Riley
Jan 3 at 11:33
exactly the same just with a different prop binding eg
@Html.DropDownListFor(x=>x.Control3, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
, @Html.DropDownListFor(x=>x.Control4, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
– L Riley
Jan 3 at 11:33
If you put a breakpoint, do
Control4
and Control5
have the values you expect?– Dan Dumitru
Jan 3 at 11:40
If you put a breakpoint, do
Control4
and Control5
have the values you expect?– Dan Dumitru
Jan 3 at 11:40
When the record comes from the db 4 & 5 have the values i'm expecting so its definitely not a source data issue but a display or binding problem. They are being passed the correct values (null) but are displaying the first non null value that's displayed.
– L Riley
Jan 3 at 11:49
When the record comes from the db 4 & 5 have the values i'm expecting so its definitely not a source data issue but a display or binding problem. They are being passed the correct values (null) but are displaying the first non null value that's displayed.
– L Riley
Jan 3 at 11:49
@LRiley Make your
Control4
& Control5
props nullable
– Izzy
Jan 3 at 11:51
@LRiley Make your
Control4
& Control5
props nullable
– Izzy
Jan 3 at 11:51
|
show 1 more comment
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54020982%2fmvc-dropdownlistfor-not-reverting-to-default-value-on-null%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54020982%2fmvc-dropdownlistfor-not-reverting-to-default-value-on-null%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your 3rd and 4th DropDown, what does the
@Html.DropDownListFor
look like?– Izzy
Jan 3 at 11:07
exactly the same just with a different prop binding eg
@Html.DropDownListFor(x=>x.Control3, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
,@Html.DropDownListFor(x=>x.Control4, (IEnumerable<SelectListItem>) ViewBag.StateList, "Optional", new {@class = "form-control"})
– L Riley
Jan 3 at 11:33
If you put a breakpoint, do
Control4
andControl5
have the values you expect?– Dan Dumitru
Jan 3 at 11:40
When the record comes from the db 4 & 5 have the values i'm expecting so its definitely not a source data issue but a display or binding problem. They are being passed the correct values (null) but are displaying the first non null value that's displayed.
– L Riley
Jan 3 at 11:49
@LRiley Make your
Control4
&Control5
propsnullable
– Izzy
Jan 3 at 11:51