i want to add onchange event to my dropdownlist - c#

The problem is that I have a form, inside this form I have dropdownlist which contain name of a student which comes from a database and I want to submit form every time I select any value from this dropdownlist. When I try this code:
#Html.DropDownList("StudentId", "--Select Students--", new { onchange = "this.form.submit();" })
An error appears to me:
StudentId is a ViewBag containing List of Students come from
database in controller action

try this:
new { #onchange = "this.form.submit();" })

Related

Reloading the webpage will make the dropdownlist null

I am trying to load all the database agents into a dropdown list, and I want to insert the selected id into another table. Dropdown list function and insert functions are working, but after the page reload, the dropdown list return as null, and I am getting this error There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key AssignPerson The code is mentioned below
View
#Html.DropDownListFor(m => m.AssignPerson, (IEnumerable<SelectListItem>)ViewBag.users, "Select Assign Person", new { #class = "form-control", #required = "required" })
Controller
public ActionResult NewAccess()
{
List<Agent> countryList = _context.Agents.ToList();
ViewBag.users = new SelectList(countryList, "AgentID", "AgentName");
return View();
}

Get values from Dropdown list MVC values

I'm using the following piece of code to get a list of values.
using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials))
{
var groups = await client.Groups.GetGroupsAsync();
List<PowerBIGroups> groupList = new List<PowerBIGroups>();
foreach (var group in groups.Value)
{
groupList.Add(new PowerBIGroups() { Id = group.Id, Name = group.Name });
}
return View(groupList);
}
And in the view at the top I'm declaring the model as:
#model IEnumerable<PowerBIEmbedded.Models.PowerBIGroups>
To get the values of the dropdown list:
#Html.DropDownList("groupId", new SelectList(Model.Select(k => k.Name)), "-- Select Group --", new { #class = "form-control", onchange = #"var form = document.forms[0]; form.action= 'GetUser'; form.submit();" })
See here, I'm able to get the values of the list in a a dropdown. But how do I get the value of Id associated with it, that I'm adding in the controller? Also, how do I get the Id in the code behind?
Using this piece of code in the onChange event in the dropdown list:
onchange = #"var form = document.forms[0]; form.action= 'GetUser'; form.submit();" } I'm able to invoke the GetUser method inside the controller with the parameters as the Name property.
How can I get the value of Id here.
One more issue associated with this is that I've to run the application from index.html where the GetUser method is invoked otherwise it is giving me a 404 not found error.
How can I fix this?

How to get disabled DropDownList control value to controller

Work on asp.net mvc5.My project DropDownList work perfectly problem arise when i disabled DropDownList control then can not get value in controller.
My razor syntax is bellow:
<div class="form-group">
#Html.LabelFor(m => m.ProductName, new {#class = "col-md-3 control-label"})
<div class="col-md-3">
#Html.DropDownList("ProductID", null, "Select product", new {#placeholder = "Select product", #class = "form-control", #disabled = "disabled" })
</div>
</div>
my controller syntax to fill the above DropDownList
private void LoadProductsInViewData(object selectedValue = null)
{
var datas = productRepository.GetAll().OrderBy(p => p.Name);
ViewBag.ProductID = new SelectList(datas, "ProductID", "Name", selectedValue);
}
Why In controller create/edit event can not get the DropDownList value?
Note:I know it's disabled control default behavior,i try to use hidden column like this.Problem is how hidden column used with my approach.
You can use Jquery to retrive the value of Currently Selected item of DropList which is disabled in your case, using
var dropDownValue=("#Id_of_u_Dropdownlist").val()
and you can send the DropDownValue variable which contains value of Disable Dropdown list to Controller.
This is a normal behavior. Disabled fields cannot be posted, then it's a common pratice to add a Html.HiddenFor() to post the disabled value.
As you said, there is no "ProductId" property in your model. However, if you create a Html.Hidden("ProductId", "YourValue") and add a parameter named 'productId' in your Controller Create/Edit action, the value should be subimitted.
Create an Httpost Action and then use FormCollection Object like this
[HttpPost]
public ActionResult GetDropDownValue(FormCollection Form)
{
string dropdownValue=form["ID_of_dropdownlist"].toString();
}

MVC 5 DropDownList lookup text

In my ViewBag there is object that contains Id,First name and Last name. I want to display DropDownList that has Id as value and "First name"+" "+"Last Name" as text.
#Html.DropDownListFor(model => model.Id, new SelectList(ViewBag.person, "Id","Last Name"), new { #class = "form-control" })
This is the line of code that I use currently, how can I modify it to display the right info?
You have to modify your controller and model to send the concatenated text to the View, or create a new SelectList in the View itself that concatenates the values before passing it to the DropdownListFor.
What you are trying to achieve would be possible if SelectList had an overload with a callback for formatting, but this isn't possible.

Can't show views every time when select value on drop down list

Select group <%=Html.DropDownList("GroupsDropDownLst",
(IEnumerable<SelectListItem>)ViewData["GroupsDropDownLst"])%>
<fieldset>
<legend>Devices And Accessories</legend>
<p>Devices:</p>
<%= Html.Action("ReadXMLDevices", "ImportXML",
new { groupID = Html.Encode(ViewData["GroupsDropDownLst"]) })%>
<p>Accessories:</p>
<%= Html.Action("ReadXMLAccessories", "ImportXML",
new { groupID = Html.Encode(ViewData["GroupsDropDownLst"]) })%>
</fieldset>
I need to show actions ReadXMLDevices and ReadXMLAccessories every time when select some value on drop down list. Any solutions?
Try this if you want to submit the form when the user selects an item in the dropdownlist.
<%=Html.DropDownList("GroupsDropDownLst", (IEnumerable<SelectListItem>)ViewData["GroupsDropDownLst"], new { onchange="this.form.submit();" })%>
I would also include a submit button, if the user has no javascript enabled.
GroupsDropDownLst as the first Parameter for the dropdownlist doesn't look right. The variable that holds the selected group (ie GroupID) should be here. It is also used in your action links.
<%= Html.ActionLink("ReadXMLAccessories", "ImportXML",
new { groupID = Html.Encode(ViewData["GroupID"]) })%>

Categories

Resources