When I post the form to the Controller, the binding on the VehiclePlate in asp-for gets the value of the select option (#vehicle.Category) but I want to get the text of the option (#vehicle.Plate) in the Controller. How can I do that?
This is my select:
#model Exam
...
<select asp-for="VehiclePlate" id="listVehicle" data-init-plugin="select2" style="width: 100%">
#foreach (var vehicle in ViewBag.Vehicles)
{
<option value="#vehicle.Category">#vehicle.Plate</option>
}
</select>
And this is my Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create(Exam exam)
{
...
}
The select tag has an asp-items attribute which you can set to a SelectList, e.g
<select asp-for="VehiclePlate" asp-items='new SelectList(ViewBag.Vehicles, "Plate", "Plate")'>
</select>
The "Plate", "Plate" part means, that the select uses Plate as value and as text for the dropdown.
Related
This is the code in View Section:
<select name="country">
<option value="0">Select</option>
#foreach (var country in countries)
{
<option value="#country.CountryId"> #country.CountryName</option>
}
</select>
Code to get value from Controller:
public ActionResult IndexPost()
{
ViewBag.Countries = CountryRepository.GetCountries();
if (Request["btnSubmitCountry"] != null)
{
string country = Request["country"];
ViewBag.Msg = "You have selected " + country;
}
return View();
I wanted to Request to get value the name of the Country too, but right now it only gets the id. Can you please help me to get the id and name both using this only this method.
Because select returns the value of the value,But you can use the hidden tag to store the text value.
E.g:
<form onsubmit="var sel=document.getElementById('country');document.getElementById('country_text').value=sel.options[sel.selectedIndex].text;">
<select name="country" id="country">
<option value="0">Select</option>
#foreach (var country in countries)
{
<option value="#country.CountryId"> #country.CountryName</option>
}
</select>
<input type="hidden" name="country_text" id="country_text" />
</form>
get text:
string countryText = Request["country_text"];
I need to bind the model with the dropdownlist to create a custom sorting filters.
My view receives the "#model PagedList.IPagedList<EmployeeInvestments.Models.Registration>" as model.
A part of My View:-
#model PagedList.IPagedList<EmployeeInvestments.Models.Registration>
#using PagedList.Mvc;
#{
ViewBag.Title = "AdminDashboard";
}
<div>
Financial Year:
<select>
#foreach(var item in Model)
{
<option>#Html.ValueFor(ModelItem=>item.FinancialYear)</option>
}
</select>
Name:
<select>
#foreach (var item in Model)
{
<option>#Html.ValueFor(ModelItem => item.Name)</option>
}
</select>
Employee ID:
<select>
#foreach(var item in Model)
{
<option>#Html.ValueFor(ModelItem=>item.employeeID)</option>
}
</select>
</div>
I tried the above approach for getting items in dropdownlist but I need unique values in the dropdownlist.
The image shows what exactly I want to achieve in View
I have created a table in the view that lists all users and certain details. So, I need to sort the users based on the filters available in dropdownlist.
Please refer to image for getting a clear picture of what I need to
achieve.
To get Unique date you can use groupby
<select>
#foreach(var item in Model.GroupBy(i=>i.FinancialYear).Select(x => x.Key))
{
<option>#Html.ValueFor(ModelItem=>item)</option>
}
</select>
To filter by distinct year :
Financial Year:
<select>
#foreach(var item in Model.Select(x => x.FinancialYear).Distinct())
{
<option>#Html.ValueFor(ModelItem=>item.FinancialYear)</option>
}
</select>
I want dropdown auto select value from DB while loading. For example i have values like this:
<option value="1">Mango</option>
<option value="2">Orange</option>
but I want when dropdown load orange selected because in previous form I already select orange.
<select class="form-control" data-plugin="select2" id="ddl_fruit" name="ddl_fruit" disabled="false" data-select2-id="2" tabindex="-1">
<option value="">Select fruit</option>
#if (ViewBag.fruitname!= null)
{
foreach (var item in ViewBag.fruitname)
{
<option value="#item.ID">#item.fruitname</option>
}
}
</select>
I am using ADO.NET and using not html.dropdown.
How do I solve the problem?
foreach (var item in ViewBag.fruitname)
{
if(#item.fruitname == "Orange")
<option value="#item.ID" selected = "selected">#item.fruitname</option>
else
<option value="#item.ID">#item.fruitname</option>
}
if you do not want to use if condition or html helpers then you use Jquery to select specific item.
Note: if you are using sections please put this code in it.
$(function(){ $('#ddl_fruit').val('2') // for orange })
Pass selected value in ViewBag.SelectedFruit from Controller to View.
In View:
foreach (var item in ViewBag.FruitName)
{
<option value="#item.Id" #(ViewBag.SelectedFruit == item.FruitName ? "selected" :"")>#item.FruitName</option>
}
Below is my razor view. I am populating my dropdown from my controller's passed list.
I need to send item that was selected from the dropdown list to send it to my api.
<div id="body">
#model List<Data>
<select>
#foreach(var item in Model)
{
<option value="#item.value" >#item.value</option>
}
</select>
<form id="sendstuff">
#*TODO need to send what was selected in the dropdown here*#
<input class="btn" type="submit" id="createAccount" value="Send Stuff" />
<br/>
<div id="apiresponse"></div>
</form>
</section>
</div>
<script src="/Scripts/jquery-1.9.0.js"></script>
<script type="text/javascript">
$(function () {
$("#sendstuff").submit(function (event) {
event.preventDefault();
var options = {
url: "api/CreateSomeAccount",
type: "POST",
data: $('#sendstuff').serialize()
};
$.ajax(options).done(function (data) {
$("#apiresponse").html(data);
});
return false;
});
});
</script>
Jscript here sends json post request to web api and grabs apiresponse back from the api and replaces the view's #apiresponse. What I want to do is to
send the option that has been selected in the dropdown and send that option to this json ajax post request.
Here is the controller code. It is a json file and passing the list of "Data" from the file to the view.
namespace Something.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
using (StreamReader r = new StreamReader(System.IO.File.OpenRead(WebConfigurationManager.AppSettings["JsonFileLocation"])))
{
string jsonData = r.ReadToEnd();
List<Data> testsNamesList = JsonConvert.DeserializeObject<List<Data>>(jsonData);
return View(Data);
}
}
}
}
try this
#using (Html.BeginForm())
{
<select name="dropdownValue">
#foreach (var item in Model)
{
<option value="#item.value">#item.value</option>
}
</select>
<input type="submit" value="submit" />
}
and get the value by add string parameter called dropdownValue to your action method
I have come up with one solution using javascript. Basically when the dropdown is selected, it populates the textbox with the dropdown selection and when I submit, the populated field in the textbox will be sent to api.
$('#dropdown').change(function () {
$('#textbox').val($(this).find(":selected").text());
});
This is not the ideal solution, but if anyone else has better solution please feel free to answer.
I am trying to bind the drop down in MVC In model but i am getting error My code is as follows:
private IEnumerable<System.Web.Mvc.SelectListItem> _property;
public IEnumerable<System.Web.Mvc.SelectListItem> TestPapers
{
get
{
Controller _controller = new Controller();
_property = _controller.BindTestPaperDropdown(string.Empty);
return _property;
}
set
{
_property = value;
}
Can any body say where i am wrong?
This could also work :
<select name="sdfsdf">
#foreach selectOption in ViewBag['SelectOptions']
<option value="#selectOption.Value">#selectOption.Text</option>
#endforeach
</select>
Just gotta put everything in the viewbag at the controller
(The example is in Razor)
With aspx it would be something like this i think ... but it's been a while since i wrote any aspx so i might miss something but that's the main idea
<select name="sdfsdf">
<% foreach(var p in products) { %>
<option value="<% p.Value %>"> <% p.Text %> </option>
<% } %>
</select>
I am sharing a link where you can explore various ways to bind dropdown list in mvc.
http://www.c-sharpcorner.com/UploadFile/deveshomar/ways-to-bind-dropdown-list-in-Asp-Net-mvc/