DropDownList Model Binding ASP.NET MVC in a PagedList View - c#

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>

Related

Wrong bindind on select option ASP.NET MVC

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.

Dynamic loading a list from a model MVC 4

so I'm still pretty new in ASP.net MVC design. I am facing a small problem, which might have already been answered, but don't seem to find an answer that will fit my purpose. Here is what's happening:
Say I have a model with 2 links: Category & Item, and it's a 1 to many relation. What I want to do is generate a list containing Category.Title (which I have completed, using the code following) and clicking on the each list item will generate a list of all Item.Title values of that category. The base idea of the html is:
<div id="cat"><!--float = left-->
<ul>
<li>List<li>
</ul>
</div>
<div id="item"> <!--float = right -->
<ul>
<li>List<li>
</ul>
</div>
Here is how everything is linked up so far:
public ActionResult Equipment()
{
ViewBag.Cat= getCat(1); // Will return a list of categories of ID 1
return View();
}
As for the view:
#{ List<X.Models.Category> catX = ViewBag.Cat; }
BODY:
<div id="pl_categories">
<ul id="pl_ec_ul">
#foreach (X.Models.Category item in catX)
{
<li id="pl_ec_li">
<div id="pl_ec_liItem">
#item.catTitle
</div>
</li>
}
</ul>
</div>
<div id="pl_values">
<ul id="pl_ev_ul">
<li id="pl_ev_li">
<div id="pl_ev_liItem">
???
</div>
</li>
</ul>
I have seen a bunch of examples, but they all involve dropdowns or what not. Here are a quick checklist of what I am trying to accomplish:
On page laod, default selected category will be the first in the list. If list is null, a simple "No Categories" is shown. Edit: if the category is empty, it will show No Items in Category.
On clicking an item in the category it will show the items of that category under pl_values.
I have tried to make this is small as possible but there are a lot more factors I need to consider. However, if I can solve this, I am certain to figure out the rest. If something however is not clear, please let me know and I can try and clarify it. Any help will be appreciated. Thank you.
For your item 1, you could try something like this (assuming your OK using jQuery):
$(document).ready(function() {
var count = $("#pl_ec_ul li").length;
if(count <= 0) {
$("#pl_categories").html("<p>No Categories</p>");
}
});

.NET MVC 4 - Button actions in a foreach loop

I've currently got a View like this:
#using (Html.BeginForm("Compare", "Carousel", FormMethod.Post))
{
#foreach (var product in Model.Products)
{
<tr>
<td>#product.Provider.Name</td>
<td>£#Math.Round(product.MonthlyPremium, 2, MidpointRounding.AwayFromZero)</td>
<td>#Html.ActionLink("More Details", "MoreDetails", new { id = product.ProductId })</td>
<td><button name="compare" value="#product.ProductId">Compare</button</td>
</tr>
}
}
Controller:
[HttpPost]
public ActionResult Compare(ProductsWithDetailModel model) // This is a guess, Model may not persist?
{
// Code here
return View("Index", model);
}
What I actually want is, when a button is clicked, the Controller is going to be called and the product that has been selected (the button clicked) will be added to a list of items.
How do I actually find out which button has been clicked in the Controller? How come I cant find any tutorials out there on the web that have done this before, surely its basic functionality? Or am I approaching it wrong?
ASP.NET MVC takes all inputs and send it by a verb to an action. The bind of these controls is made by the name attribute on your View and the value is made by depending of what control we are talking about (text, checkbox, radio, select, button etc...). You could have a a property on your ProductsWithDetailModel called compare and it would recevie the value of the button you've clicked.
How about having 1 form per item?
E.g.
#foreach (var product in Model.Products)
{
<tr>
<td>#product.Provider.Name</td>
<td>£#Math.Round(product.MonthlyPremium, 2, MidpointRounding.AwayFromZero)</td>
<td>#Html.ActionLink("More Details", "MoreDetails", new { id = product.ProductId })</td>
<td>
#using (Html.BeginForm("Compare", "Carousel", FormMethod.Post))
{
<input type="hidden" name="productId" value="#product.ProductId" />
<button name="compare">Compare</button>
}
</td>
</tr>
}
use onclick. set js hidden value in loop. in controller check.

Binding Dropdown in MVC 2.0

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/

Setting the first default value in a drop down list MVC

I want to be able to set the first value in my dropdown list as "Select a poll", and then load all my select items from my viewdata respectively. How would I go about doing that? My block of code right now looks something like this:
<select name="ddlPolls" id="ddlPolls">
<% foreach (var item in (string[][])ViewData["returnPolls"])
{ %> <option value="<%= item[0] %>"><%= item[1] %></option><% } %>
</select>
This returns a dropdown list of my polls, but I want the first value in that dropdown list to be "Select a poll" so that I can use jquery to perform .change ajax functions to load another dropdown list. Any help is much appreciated!! :D
Just write your code as follow:
<select name="ddlPolls" id="ddlPolls">
<option value="0">Select a poll</option>
<% foreach (var item in (string[][])ViewData["returnPolls"])
{ %> <option value="<%= item[0] %>"><%= item[1] %></option><% } %>
</select>

Categories