I have some problems with a multiple dropdownlist, while loading its showing a textbox and them it load the dropdownlist control:
this is the code in the View:
#Html.DropDownListFor(m => #m.SelectedInfractionsId,
new MultiSelectList(Model.InfractionList, "Value", "Text", #Model.SelectedInfractionsId),
new {
id = "SelectedInfractionsId",
#class = "form-control multiselect-dropdown",
multiple = "true",
data_placeholder = "Select Users"
})
This is the code in the Controller:
List<SelectListItem> Listofinfractions = new List<SelectListItem>();
var infractions = _context.InfractionsCategories.OrderByDescending(x => x.Id == 1);
foreach (var infraction in infractions)
{
Listofinfractions.Add(new SelectListItem()
{
Value = infraction.Id.ToString(),
Text = infraction.Name
});
}
Listofinfractions.Insert(0, new SelectListItem()
{
Text = "----Select----",
Value = string.Empty
});
Looks like the image down below:
DropDownListFor control while is loading
once is loaded the control looks like :
DropDownListFor control when is loaded
Related
I have an Html Form in a Wizard Wrapper with 2 for loops to display the model. Each time the "Next button" is clicked, I need to save the specific unit.
Here is a part of the form (it is huge so I didn't want to post more than is necessary):
#using (Html.BeginForm("SaveAssessmentUnitScores", "Management", FormMethod.Post, new { id = "SaveUnitScores" }))
{
#Html.HiddenFor(x => x.AttemptId)
#Html.HiddenFor(x => x.AssessorId)
#Html.HiddenFor(x => x.EmployeeCompanyId)
#Html.HiddenFor(x => x.RequirementId)
for (var i = 0; i < Model.Units.Count(); i++)
{
var block = Model.Units[i];
<div class="kt-wizard-v2__content" id="CurrentUnit" data-value="#block.UnitId" data-ktwizard-type="step-content" #(i == 0 ? "data-ktwizard-state='current'" : "")>
I need the "data-value" from the "CurrentUnit" div.
I tried using the wizard atrributes, then I tried putting an id on the div and using "getAttribute", neither works:
var form2 = $('#SaveUnitScores');
wizard = new KTWizard('kt_wizard_v2', options);
wizard.on('beforeNext', function (wizardObj) {
let sec = form2.find('[data-ktwizard-state="current"]');
let curPg = sec.attr("value");
var curUnit = form2.find('#CurrentUnit');
var unitId = curUnit.attr('data-value');
I do not receive any javascript errors but "unitId" and "curPage" are both empty strings. Any assistance is greatly appreciated.
I am trying to give the background color to the textbox conditionally but not working when I assign the variable columnstyle to the textbox. Here is the html code
var columnstyle = "text-align:center;vertical-align:central;background-color:#FFBF00";
if (Model.attendanceLogList[i].IsProtected)
{
isdisabled = true;
styleset = "background-color:#90EE90";
columnstyle = "text-align:center;vertical-align:central;background-color:#90EE90";
}
<td>
#Html.TextBoxFor(m => m.attendanceLogList[i].NormalHrs, new { #class = "form-control input-sm NormalHrsl", #style= "#(columnstyle)", #Value = Model.attendanceLogList[i].NormalHrs, onchange = "CalculateTotal()" })
<td>
You can try something like this
var columnstyle = Model.attendanceLogList[i].IsProtected == false ? "text-align:center;vertical-align:central;background-color:#FFBF00" : "text-align:center;vertical-align:central;background-color:#90EE90";
...
I have a problem. I have a short list of clients and for each of them I want to display a pin and Popup window, which will be displayed after clicking the info window. However, I have no idea how to connect it.
Part of my code:
List<Client> lstClients = new List<Client>
{
new Client(1, "Firma 1", "Wspólna 10", "123-123-23-23", "F1", true),
new Client(2, "Firma 2", "Marszałkowska", "456-456-56-45", "F2", false),
new Client(3, "Firma 3", "Jerozolimskie 57", "789-789-89-78", "F3", true),
new Client(4, "Firma 4", "Koszykowa 10", "234-423-43-23", "F4", false)
};
foreach (Client client in lstClients)
{
var geoadres = client.Address;
var locations = await Geocoding.GetLocationsAsync(client.Address);
var location = locations?.FirstOrDefault();
ListPin = new Pin
{
Type = PinType.Place,
Label = client.FirmName,
Address = client.Address,
Position = (new Position(location.Latitude, location.Longitude)),
Rotation = 33.3f,
Tag = client.Tag
};
map.Pins.Add(ListPin);
}
void InfoWindow_Clicked(object sender, InfoWindowClickedEventArgs e)
{
PopupNavigation.Instance.PushAsync(new ShowPopup());
}
I will be grateful for any help.
By clicking on the pin need to show the pop up window, you can implement like this;
foreach (Client client in lstClients)
{
var geoadres = client.Address;
var locations = await Geocoding.GetLocationsAsync(client.Address);
var location = locations?.FirstOrDefault();
ListPin = new Pin
{
Type = PinType.Place,
Label = client.FirmName,
Address = client.Address,
Position = (new Position(location.Latitude, location.Longitude)),
Rotation = 33.3f,
Tag = client.Tag
};
map.Pins.Add(ListPin);
// tap event from map pinview -> Callout action
map.Pins[i].Clicked += (sender, e) =>
{
System.Diagnostics.Debug.WriteLine(((Pin)sender).Type);
DisplayAlert(((Pin)sender).Label, ((Pin)sender).Address, "OK");
};
}
add the code inside for-loop
I want to create selectlist dynamically by traversing child nodes with 'language' property. So, want to add this property value as a select list item only if it is not added previously.
I have following code.
#{
var litem = new List<SelectListItem>();
litem.Insert(0, new SelectListItem { Selected = true, Text = "All", Value = "" });
foreach (var i in Model.Content.Children.Where("Visible"))
{
//if (i.GetProperty("language").Value != "")
if (i.GetProperty("language").Value != "")
{
string langstr = i.GetProperty("language").Value.ToString();
SelectListItem item = new SelectListItem { Selected = false, Text = langstr, Value = langstr };
if ((!(litem.Contains(item))))
{
litem.Add(item);
}
}
}
var slang=new SelectList(litem);
#Html.DropDownList("drpLang", #slang, new { #class = "dropdown"})
}
But it is not able to check the same item present in list. What is going wrong?
if i understand you correctly, problem that litem contain duplicate,
this because you create new instance of object item and than check is list contain new instance (you not check for same property, you check for exactly same object).
change this line:
if ((!(litem.Contains(item))))
for something like this:
if (litem.All(i => i.Value != item.Value))
or if you need unique pair:
if (litem.All(i => i.Value != item.Value && i.Text!= item.Text))
also recommend move all login in controller and use viewbag for DropDownList
So I have this in my view(I have broken it out into a foreach so I could debug it properly). And according to the objects that are created it looks good. I get 1 item that has the selected true.
#{
var useritems = new List<SelectListItem>();
foreach (var si in Model.UserList)
{
if (Model.CurrentUser.Id.Equals(si.Id))
{
useritems.Add(new SelectListItem { Selected = true, Text = si.Username, Value = si.Id.ToString(CultureInfo.InvariantCulture) });
}
else
{
useritems.Add(new SelectListItem { Text = si.Username, Value = si.Id.ToString(CultureInfo.InvariantCulture) });
}
}
}
What I want to do here is check for the current(logged on) user and set the select default to him/her.
#Html.DropDownListFor(model => model.Creator, useritems, new { id = "CreatorDDL", })
This does not however set any of the objects in useritems to selected. Even tho in the debugger it shows that 1 item has selected: true
Somehow it just shows the first item in the list.
Any ideas? or am I just tired and missing something really easy?
Set model.Creator value in addition to Selected in SelectList.
model.Creator = Model.CurrentUser.Id;
It's better to do such things in your Controller, not in your View.