How can we clear/reset the selected item on a Radzen DropDown?
I did not use bind-Value to get the selected item.
Instead, I used Change() to get the selected object.
However, if I select one item on the DropDown and change the content of tempLocations, the selected item itself will not be cleared.
View:
<RadzenDropDown Name="Location" TValue="string" Data=#tempLocations Change=#(args => LocationChange(args))>
<Template Context = "location">
#($"{location.id}:{location.name}")
</Template>
</RadzenDropDown>
I guess there will be no solution expect using bind-Value.
Here is my updated coding:
<RadzenDropDown #bind-Value=#tempSelectedLocation Name="ALocation" Data=#tempLocations Change=#(args => LocationChange(args))>
<Template Context = "location">
#($"{location.pressure} {location.name}")
</Template>
</RadzenDropDown>
tempSelectedLocation is a class object.
Related
Working on MVC 5 app.
I have a table with a bunch of columns. One column contains a radio button list of items. Since this is an "edit" view I need to pre-populate the selected radio button properly. (ie select the item that's in the db)
Part of my controller code...
newVmRecord.SurveyAnswerOption =
new SelectList(db.SurveyAnswerOptions,
"AnswerOptionText", "AnswerOptionText", ar.SurveyAnswer);
Here's the razor:
#foreach (SelectListItem item in
(IEnumerable<SelectListItem>)Model[i].SurveyAnswerOption)
{
#Html.RadioButtonFor(modelItem => Model[i].SelectedSurveyAnswer,
Model[i].SurveyAnswerOption, new { #Checked =
Model[i].SelectedSurveyAnswer}) #item.Text
}
The available options are 'yes', 'no' and 'n/a'. In the database, for example, if 'no' was already selected then it should be pre-selected when this view loads. The radio buttons appear correctly, but, the #Checked doesn't work correctly.
By the way, I am sure there is existing data because I put this line of code as the first line the foreach....
#Html.DisplayTextFor(modelItem => Model[i].SelectedSurveyAnswer);
Any suggestions? Thanks!
Please try this:
#foreach (SelectListItem item in (IEnumerable<SelectListItem>)Model[i].SurveyAnswerOption)
{
#Html.RadioButtonFor(modelItem => Model[i].SelectedSurveyAnswer,#item.Value, Model[i].SelectedSurveyAnswer == item.Value ? new {Checked = "checked"} : null) #item.Text
}
I am trying to fetch a field called footer from the Item FooterComponent and want to display.This is the code I have tried but cannot fetch and display.
cs code:
Item footerText=Sitecore.content.Database.GetItem(GUID);
string MyFooter=FooterText["Footer"];
txtFooter.Item = MyFooter;
ascx code:
<sc:fieldrenderer runat="server" id="txtFooter" fieldname="Footer"/>
You should not set a string value of the field to the field renderer. Just set correct item and field name:
Item footerTextItem = Sitecore.content.Database.GetItem(GUID);
txtFooter.Item = footerTextItem;
<sc:fieldrenderer runat="server" id="txtFooter" fieldname="Footer"/>
See more information here: https://sitecoresandbox.com/tag/fieldrenderer/
Also read What's a good way to set the Item or DataSource attribute of a FieldRenderer?
In a SelectItem, I get back a collection of objects/values for use in a dropdown list. I need to find a specific item in the returned list and set the Selected property to true if it matches a value (stateCode) that is passed in... I have this which, obviously, doesn't work. The line after the //psuedo comment is where I'd like to make the change. Any idea on how to do this? I don't seem to have access to the properties I want to change...
SelectList retVal = new SelectList(this.db.States.Where(x => x.Col1== someVal), "ValueCol", "TextCol", someVal);
foreach (SelectListItem item in retVal) {
//psuedo
retVal[item.Index].Selected = item.Value == stateCode ? true : false;
}
return retVal;
As requested, here is how I'm rendering the dropdown that this SelectItem is bound to:
<div class="select" id="StateDropDown">
<label for="StateProvince">#Resources.Expert.State_or_Province:</label>
#Html.DropDownListFor(x => x.StateProvince, Model.StateList)
</div>
To assign a selected value to the drop down you don't need to set the value in your select list. all you need to do is set the value on your model when using a for helper. So for your drop down
#Html.DropDownListFor(x => x.StateProvince, Model.StateList)
make sure that you set StateProvince to the value that you want to select. Also make sure that the value of StateProvince Matches a value that is in your SelectList Model.StateList.
It would be appropriate to pass selected value to #Html.DropdownList method.
Assuming stateCode is state you want to be selected.
Like this.
<div class="select" id="StateDropDown">
<label for="StateProvince">#Resources.Expert.State_or_Province:</label>
#Html.DropDownListFor(x => x.StateProvince, Model.StateList, stateCode)
I have built a mutli-select checkbox list
foreach (var item in Model)
{
#Html.HiddenFor(i => item.SectionID)
#Html.CheckBoxFor(i => item.IsChecked)
#Html.LabelFor(i => item.IsChecked, item.SectionTitle)
<br />
}
While this generates a checkbox for each item and the proper text lable next to each box the problem comes that no matter which label you click it only toggles the first check box. How can I tie each label to the appropriate checkbox for this setup?
#Html.CheckBoxFor(i => item.IsChecked,new{id=item.SectionID})
The following block of code is used to make the selected value of a dropdownlistfor persist after submit:
#Html.DropDownListFor(x => Model.Value, new SelectList(Model.Values, #Model.Value))
In this block of code, Model.Values is a list of strings and the #Model.Value specifies which value is selected.
This block of code works great but I'm trying to do the same thing with a list of SelectListItems (rather than a list of strings). In the following code Model.Values is a list of SelectListItems.
#Html.DropDownListFor(x => Model.Value, Model.Values, Model.Value,
new
{
id = "ValueDD", onchange = "this.form.submit()"
})
Is it possible to use #Model.Value as I did in the first code block to set the selected value?
SelectListItem has a property Selected that needs to be set for the selected item, see http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlistitem.ASPX
So in your controler you can just set the Selected property of the item that needs to be selected, assuming you are using the controler to build the list of Model.Values