and I have a dropdown list which looks something like
@Html.DropDownListFor(model => model.ChildItems[i].RestrictionValue,
from Project.Core.Enums.Items.Restriction state in Enum.GetValues(typeof(Project.Core.Enums.Items.Restriction))
select new SelectListItem() { Text = state.ToString(), Value = ((int)state).ToString() }
,"Select", Model.ChildItems[i].RestrictionValue
)
but when there is no value select I get the error
Nullable object must have a value.
is there a way that the selected field can have a 0 value? or is there another work arround
@Html.DropDownListFor(model => model.ChildItems[i].RestrictionValue,
from Project.Core.Enums.Items.Restriction state in Enum.GetValues(typeof(Project.Core.Enums.Items.Restriction))
select new SelectListItem() { Text = state.ToString(), Value = ((int)state).ToString() }
,"Select", null )
The syntax for DropDownListFor you used should be as below:
EnenDaveyBoy
Participant
1466 Points
1157 Posts
dropdown list default value
May 09, 2012 02:07 PM|LINK
I have a mode which has a int? value
and I have a dropdown list which looks something like
@Html.DropDownListFor(model => model.ChildItems[i].RestrictionValue,
from Project.Core.Enums.Items.Restriction state in Enum.GetValues(typeof(Project.Core.Enums.Items.Restriction))
select new SelectListItem() { Text = state.ToString(), Value = ((int)state).ToString() }
,"Select", Model.ChildItems[i].RestrictionValue
)
but when there is no value select I get the error
Nullable object must have a value.
is there a way that the selected field can have a 0 value? or is there another work arround
abdu292
Participant
1555 Points
372 Posts
Re: dropdown list default value
May 09, 2012 02:30 PM|LINK
use .value property for the nullable value.
eg.
int? StateID;
then use
StateID.Value;
With best regards,
EnenDaveyBoy
Participant
1466 Points
1157 Posts
Re: dropdown list default value
May 09, 2012 05:38 PM|LINK
sorry I am a bit lost can you show me what you mean
Young Yang -...
All-Star
21742 Points
1825 Posts
Microsoft
Re: dropdown list default value
May 15, 2012 09:35 AM|LINK
Hi
Whick value is int? ? Could you explain more?
Regards
Young Yang
Feedback to us
Develop and promote your apps in Windows Store
Ict-Guy
Member
106 Points
19 Posts
Re: dropdown list default value
May 15, 2012 11:31 AM|LINK
try
@Html.DropDownListFor(model => model.ChildItems[i].RestrictionValue,
from Project.Core.Enums.Items.Restriction state in Enum.GetValues(typeof(Project.Core.Enums.Items.Restriction))
select new SelectListItem() { Text = state.ToString(), Value = ((int)state).ToString() }
,"Select", null
)
The syntax for DropDownListFor you used should be as below:
BlackShadows
Member
4 Points
28 Posts
Re: dropdown list default value
May 15, 2012 11:55 AM|LINK
try do it with Json and Ajax its simple way to do it.
http://www.ienablemuch.com/2011/05/cascading-dropdownlist-using-aspnet-mvc.html?showComment=1333529459280#c4027748902216758509
Doctor Sid
Member
156 Points
46 Posts
Re: dropdown list default value
May 15, 2012 12:46 PM|LINK
where exactly does the exception occur?
i guess it occurs when accessing the Value property of a nullable that doesn't hold a value
if 'nullable' doesn't mean anything to you, plz google it