How to populate dropdown list base on another? - asp.net-mvc

View:
<div class="form-group">
<label for="country" class="control-label col-lg-5">#Html.Label(#BetXOnline.TranslationProvider.Instance.GetTranslationMessage("COUNTRY")):#Html.ValidationMessageFor(m => m.Register.SelectCountryId, null, new { #class = "required" })</label>
<div class="col-lg-7">
#Html.DropDownListFor(m => m.Register.SelectCountryId, Model.Register.Country, "Select country", new { id = "country", #class = "form-control" })
</div>
</div>
<!-- Region-->
<div class="form-group">
<label for="region" class="control-label col-lg-5">#Html.Label(#BetXOnline.TranslationProvider.Instance.GetTranslationMessage("REGION")):#Html.ValidationMessageFor(m => m.Register.SelectRegionId, null, new { #class = "required" })</label>
<div class="col-lg-7">
#Html.DropDownListFor(m => m.Register.SelectRegionId, Model.Register.Region, "Select region", new { id = "region", #class = "form-control" })
</div>
</div>
<!-- City-->
<div class="form-group">
<label for="region" class="control-label col-lg-5">#Html.Label(#BetXOnline.TranslationProvider.Instance.GetTranslationMessage("CITY")):#Html.ValidationMessageFor(m => m.Register.SelectCityId, null, new { #class = "required" })</label>
<div class="col-lg-7">
#Html.DropDownListFor(m => m.Register.SelectCityId, Model.Register.City, "Select region", new { id = "region", #class = "form-control" })
</div>
</div>
Controller:
model.Register.Country = new SelectList(manager.GetCountries(), "Id", "Name");
model.Register.Region = new SelectList(manager.GetRegions(model.Register.SelectCountryId), "Id", "Name");
model.Register.WebTypeOfLicense = new SelectList(CommonDbService.GetAllLicense(), "Id", "Name");
model.Register.City = new SelectList(manager.GetCities(model.Register.SelectRegionId), "Id", "Name");
Function:
IEnumerable<ICountry> GetAllCountries();
ICountry GetCountry(int countryId);
IEnumerable<IRegion> GetRegions(int countryId);
IRegion GetRegion(int regionId);
IEnumerable<ICity> GetCities(int regionId);
ICity GetCity(int cityId);
Main idea is when i select country to get state and when i click on state to get cities...State recives id of country so i need to populate state with id of country to get states and then cites recive id of state to populate cities

You need some javascript function that will fire on dropdown change event and bring the respective child element of selected parent in dropdown. once you receive response fill the child dropdown with latest value you have.
one of simple example : sample

Related

multiple select inside dropwdownlist mvc

I need multi select checkbox inside dropdown list mvc
<div class="form-group">
<label for="Product" class="required">Product</label>
#Html.DropDownList("SellerSKUid", null, new { #id = "txtOeCustomer",#name = "SellerSKUid", #class =
"form-control", placeholder = "Select SellerSKUid"})
</div>
<div class="form-group">
<label for="Product" class="required">Product</label>
#Html.DropDownList("SellerSKUid", null, new { #id = "txtOeCustomer",#name = "SellerSKUid", #class =
"form-control", placeholder = "Select SellerSKUid"})
</div>

Asp.net MVC Checkbox binding with string format

<div class="form-group">
<label>Quoted With New Combo Endorsement</label>
#Html.DropDownListFor(a => a.Quoted_With_New_Combo_Endorsement, new[]
{
new SelectListItem {Text = "Yes", Value = "Yes"},
new SelectListItem {Text = "No", Value = "No"},
}, "Select the Value", new { style = "width:250px", #class = "form-control" })
</div>
How to convert this code to the checkbox for(Yes/NO)?
Reminder: datatype can't be changed to bool
I've done this in a project and it's working fine.
<div class="form-group">
<label class="col-md-2 control-label">Scan in Colour?</label>
<div class="col-md-10">
<div class="radio-group">
#Html.RadioButtonFor(m => m.ScanInColour, "Yes",
new { #id = "ScanInColourYes", #class = "form-control" })
<span>Yes</span>
#Html.RadioButtonFor(m => m.ScanInColour, "No",
new { #id = "ScanInColourNo", #class = "form-control" })
<span>No</span>
</div>
</div>
</div>
Shouldn't it be a radio-button because it either yes or no?
Instead of the #Html.DropDownListFor use:
#Html.RadioButton("YesNo","Yes")
#Html.RadioButton("YesNo","No")

How do I post varied length list to controller?

MVC4. I have a dynamic list on a view, a new textbox is added with button click (which adds a partialView) so user can enter a list of stuff. That is contained in a form element with a submit button.
In the controller I have tried three different types:
[HttpPost]
public ActionResult Index(IEnumerable<AccessoryVM> form)
{
-- form is NULL
[HttpPost]
public ActionResult Index(AccessoryVM form)
{
-- form has only the first item in the list
[HttpPost]
public ActionResult Index(FormCollection form)
{
-- seems to receive the list, but having trouble getting the values
All the examples I have seen are using a for list to add an index to each item, but they aren't using a dynamic list (it has a fixed length).
What should the Controller receiving type be?
EDIT to add more detail:
Button click appends partial view:
$("#add-item").on("click", function () {
$.ajax({
url: '/Accessory/AddItem',
cache: false,
success: function (html) {
$("#form-body").append(html);
}
})
return false;
})
Partial View:
#model EmployeeHardwareRequest.Models.ViewModels.AccessoryVM
<div class="form-group col-md-3">
#Html.LabelFor(model => model.ItemDescription, htmlAttributes: new { #class = "control-label" })
<div>
#Html.DropDownListFor(model => model.AccessoryId, Model.AccessoryDdl, new { #class = "form-control" })
</div>
</div>
<div class="form-group col-md-9">
#Html.LabelFor(model => model.ProductLink, htmlAttributes: new { #class = "control-label" })
<div>
#Html.EditorFor(model => model.ProductLink, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ProductLink, "", new { #class = "text-danger" })
</div>
</div>
Main View - partial is appended to the #form-body:
#using (Html.BeginForm("Index", "Accessory", FormMethod.Post, new { #id="accessory-form", #class = "form-horizontal" }))
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div id="form-body">
<div class="form-group col-md-3">
#Html.LabelFor(model => model.ItemDescription, htmlAttributes: new { #class = "control-label" })
<div>
#Html.DropDownListFor(model => model.SelectedAccessory, Model.AccessoryDdl, new { #class = "form-control" })
</div>
</div>
<div class="form-group col-md-9">
#Html.LabelFor(model => model.ProductLink, htmlAttributes: new { #class = "control-label" })
<div>
#Html.EditorFor(model => model.ProductLink, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ProductLink, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<button id="add-item" class="btn btn-primary">Add Another Item</button>
</div>
<div class="col-md-6">
<input type="submit" value="Select Software" class="btn btn-default pull-right" />
</div>
</div>
}
Most likely, you have a binding issue. Assuming all you're posting is a collection of AccessoryVM, then the parameter should be List<AccessoryVM> form. However, in order for the modelbinder properly bind the posted values, your input names must be in a specific format, namely either form[N].Foo or just [N].Foo.
You haven't given any detail about what's going on in your view, but since these are dynamically added, you must be using JavaScript to add additional inputs to the page. If you're doing it via AJAX (returning a partial view), you'll need to pass the index to your endpoint so that it can be utilized to generate the right input names. If you're using something like Knockout, Angular, etc., you should ensure that whatever template is being used to generate the inputs takes an index into account.

Searching/Filtering on All Columns in KendoUI Grid using DropdownlistFor

I am trying to Filter/Search kendoUI grid using my custom Dropdownlist. I have been able to get a result on singe column filtering but if i try to add new dropdownlist for filtering the Grid i can't get the required result.
my problem is with logic operator and it's different values.
is it the correct way ? or any other suggestions?
I add here the images for more description and hope you to help me please. thanks
HTML dropdowns :
<html>
<div class="form-group col-md-2" style="margin-bottom:0px;">
#Html.Label("Media", htmlAttributes: (new { #class = "col-sm-4 control-label" }))
<div class="col-sm-8" style="float:none;">
#Html.DropDownListFor(model => model.MediaTypeID, (ViewData["media"] as SelectList), "select media", new { #class = "form-control btnregister chosen-select", onchange = "changes_dropdown(this.id)", data_placeholder = "select media", style = "border-bottom:none;border-radius:0px;" })
#* #Html.ValidationMessageFor(model => model.MediaTypeID, "", new { #class = "text-danger" })*#
</div>
</div>
<div class="form-group col-md-2" style="margin-bottom:0px;">
#Html.Label("Outlet", htmlAttributes: (new { #class = "col-sm-4 control-label" }))
<div class="col-sm-8" style="float:none;">
#Html.DropDownListFor(model => model.channel, (ViewData["company"] as SelectList), "select channel", new { #class = "form-control btnregister chosen-select", onchange = "changes_dropdown(this.id)", data_placeholder = "select channel", style = "border-bottom:none;border-radius:0px;" })
#Html.ValidationMessageFor(model => model.channel, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group col-md-2" style="margin-bottom:0px;">
#Html.Label("Start Date", htmlAttributes: (new { #class = "col-sm-6 control-label" }))
<div class="col-sm-6" style="float:none;">
#Html.DropDownListFor(model => model.EntryDate, (ViewData["entryDate"] as SelectList), "select date", new { #class = "form-control btnregister chosen-select", onchange = "changes_dropdown(this.id)", data_placeholder = "select date", style = "border-bottom:none;border-radius:0px;", id = "startDate" })
#Html.ValidationMessageFor(model => model.EntryDate, "", new { #class = "text-danger" })
</div>
</div>
</html>
Javascript Function and filltering:
<script type="text/javascript">
function changes_dropdown(fieldID)
{
var fieldValue = document.getElementById(fieldID).value;
$("#grid").data("kendoGrid").dataSource.filter({
logic: "and",
filters: [
{
field: fieldID,
operator: "contains",
value: fieldValue
}
]
});
}
</script>
At first you don't need to pass id of current field to changes_dropdown function. Instead you can use onchange = "changes_dropdown() and inside this function use var fieldValue = this.value
Also when you define single filter you don't need to specify logic between predicates (http://docs.telerik.com/kendo-ui/api/javascript/data/datasource#configuration-filter):
$("#grid").data("kendoGrid").dataSource.filter({
filters: [
{
field: fieldID,
operator: "contains",
value: fieldValue
}
]
});

Strange behaviour with MVC posting list

I have the following dynamically created textboxes in my view:
#for (int i = 0; i < Model.MullionList.Count; i++)
{
ItemDrops curMullItem = Model.MullionList.ElementAt(i);
<div class="form-group">
#Html.Label(curMullItem.ItemName.ToString(), new { #class = "col-sm-6 control-label" })
<div class="col-sm-6">
#Html.TextBoxFor(x => x.MullionList[i].ItemPossInfo, new { #class = "form-control" })
#Html.HiddenFor(x => x.MullionList[i].ItemName)
</div>
</div>
}
I noticed occaionally for certain types of product this was returning null when posted (for MullionList), even though i selected these dropdowns.
so it works for certain product but not others.
The even weirder part is when i remove this section further down the view it works (ie MulionList is not null when the form is posted)
<div class="form-group">
#Html.Label("Glass", new { #class = "col-sm-6 control-label", id = "glass-first-label" })
<div class="col-sm-6">
#Html.DropDownListFor(gu => gu.GlassItems[0].Value, Model.GlassTypes, "-- Select --", new { #class = "form-control glass-multi-select", id = "Glass" + 0 })
</div>
</div>
<div id="hidden-glass-select" style="display: none">
#for (int i = 1; i < Model.GlassUnitsCount; i++)
{
var glassUnit = Model.GlassUnits.ElementAt(i);
<div class="form-group">
#Html.Label(glassUnit.ToString(), new { #class = "col-sm-6 control-label" })
<div class="col-sm-6">
#Html.DropDownListFor(gu => gu.GlassItems[i].Value, Model.GlassTypes, "-- Select --", new { #class = "form-control glass-multi-select", id = "Glass" + i })
</div>
</div>
}
</div>
<div class="form-group">
#Html.LabelFor(go => go.GlazzingInfoVal, "Glazzing Option", new { #class = "col-sm-6 control-label" })
<div class="col-sm-6">
#Html.DropDownListFor(go => go.GlazzingInfoVal, Model.GlazzingOptions, "-- Select --", new { #class = "form-control" })
</div>
</div>
I think I have fixed this now.
I renamed all items (lists that started with Glass or Glazzing and it works!)
This looks like a proper MVC bug
Thanks to this answer:https://stackoverflow.com/a/16113919/66975
This has costed me several hours, GRRRR

Resources