Hello,
I have a requirement where the user needs to be given the ability to enter custom text inside a ComboBox if the value is not already there. The ComboBox is bound to a model. I'm not sure what is the best way to accomplish this. Currently, in my controller I check to see if the Id of the entered text is equal to 0, and then I update the model data. I am able to add the new value to the database, afterwards however, I don't know how to refresh/rebind the combobox to show the newly entered data.
Here's my Html-Helper code for the ComboBox:
@(Html.Kendo().ComboBox()
.Name(
"SystemType"
)
.AutoBind(
true
)
.BindTo((System.Collections.IEnumerable)ViewData[
"SystemTypes"
])
.DataTextField(
"SystemTypeName"
)
.DataValueField(
"SystemTypeId"
)
.Filter(FilterType.Contains)
.HtmlAttributes(
new
{ style =
"width: 350px; background-color: white;"
})
.Placeholder(
"..."
)
.ClearButton(
false
))
Here's the code in my Controller to add a new value:
if
(asset.SystemTypeId == 1 || asset.SystemTypeId == 0)
{
string
SystemTypeName = Request.Form[
"SystemType"
];
if
(asset.SystemTypeId == 0 && !
string
.IsNullOrEmpty(SystemTypeName))
{
SystemType systemType =
new
SystemType()
{
ProjectId = Id,
SystemTypeName = SystemTypeName
};
_repository.AddSystemType(systemType);
}
else
{
ModelState.AddModelError(
"SystemTypeId"
,
"System Type is Required"
);
}
}
Any help is greatly appreciated. Thanks.
Shawn A.