MVC Viewbag correct Result count but displaying only item 1 - sql

Trying to use a viewbag on a create form (view) to let the user pick an "item" on the dropdown.
The dropdown displays and shows correct number of rows (tested) but only displays the first item for the X number of rows.
https://i.imgur.com/2179GTD.png "Image1"
Code of the view controller below as I didn't find any answers to this.
List<SelectListItem> Contracts = (from a in
db.v_xxx_yyyy_aaaa_contracts.Where(m => m.GroupID.ToString() ==
#group).Distinct().OrderBy(a => a.ContractID).ToList()
select new SelectListItem()
{
Value = a.ContractID,
Text = a.ContractID
}).ToList();
ViewBag.ContractID = Contracts;

Try something like
var contracts = db.v_xxx_yyyy_aaaa_contracts.Where(m => m.GroupID.ToString() == #group).Distinct().OrderBy(a => a.ContractID);
ViewBag.ContractID = new SelectList(contracts, "ContractID", "ContractID"); // here second one for display

The solution I found for this specific problem is found here!
Had to bypass the viewmodel with ViewData (SelectList) for it to work as I wanted.

Related

Matching selecting dynamic content Id to list of ItemViewModel in SiteFinity

I have created a module Products and content type product. I am creating a custom widget to display a single product. I have setup my designer and once I drop the widget on a page I can select from the product list using the sf-list-selector sf-dynamic-items-selector. My issue is matching the selected item ID to the list of products my widget is pulling up. Here is the code the widget uses to retrieve all products:
var dynamicModuleManager = DynamicModuleManager.GetManager(providerName);
var contentType = TypeResolutionService.ResolveType(typeName);
var contentElements = dynamicModuleManager.GetDataItems(contentType).Where(x => x.Status == ContentLifecycleStatus.Live);
products = contentElements.ToArray().Select(p => new ItemViewModel(p)).ToArray();
This works good and pulls up the list of products. The question is how to filter this list using the selected product id from the designer. I have this and they don't match:
products.Single(p => p.DataItem.Id == Guid.Parse(selectedProductId))
How do I go from the ItemViewModel to the Id the selector is giving me?
using Feather 9.1
Went a different route. Instead of getting a list and filtering did so:
var dynamicModuleManager = DynamicModuleManager.GetManager(providerName);
var contentType = TypeResolutionService.ResolveType(typeName);
var contentElement = dynamicModuleManager.GetDataItem(contentType, Guid.Parse(selectedProductId));
product = new ItemViewModel(dynamicModuleManager.Lifecycle.GetLive(contentElement));

Filter out data from the datatable based on the selection from the user

Scenario:
I need to filter out data from the datatable based on the selection that has been made from the user. To implement this I have a tree control which represents the hierarchy of the data in the datatable. When a user unchecks a certain node in the tree. That data should be taken out from the datatable.
Question:
How do I filter "out" data from the datatable?
If I use search() method, it gives me that matched rows and I want the opposite of this. I need to take out the matched rows instead of showing them.
I tried using the following filter function but it gives me the filtered data.
table.column([column number]).data().filter(function (value, index) {});
I would appreciate any help on this.
Thanks
You could implement your own custom search function:
var table = $('#myTable').DataTable();
$.fn.dataTable.ext.search.push(
function(oSettings, aData, iDataIndex) {
var match = true;
$(".checkbox:checked").each(function(index) {
var cb = $(this);
var id = $(this).attr('id');
if (aData.toString().toLowerCase().indexOf(id) >= 0) {
match = false;
} else {
match = true;
}
});
return match;
});
Please see a demo here. Hope it helps.
The simple answer is to invert the filter. i.e. change the filter to return false where it currently returns true and vice versa.
You can also do this with search, by inverting the regex you look for.

display related field based on textbox id

I have two textboxes
<div>#Html.TextBoxFor(model=>model.Voyage_ID, new{id="VID"})<br /></div>
<div>#Html.EditorFor(model=>model.Arrived, new { #id = "arrived",})</div>
The first is updated dynamically from json based on the selected index of a dropdownlist. In the same manner, I wish to display the second field (and many more other fields that are) associated with the id captured in VID .
How can I accomplish this?
You can use jQuery .change() method:
$(document).ready(function(){
$('#VID').change(function(){
var currentValue = $(this).val(); //get current value of the input
//here comes your logic
});
})

Binding Gridview with drop down list selection MVC 5

I have a drop down list (Cities) and on selection it should be able to bind a Grid View in MVC 5
i am able to bind the drop down list and grid view but unable to handle the select event of Drop Down list. I dont know how to proceed on this I dont want to use Jquery. Below is my code
// BELOW IS THE DROP DOWN LIST CODE
#using (Html.BeginForm("BindGridView", "FacebookConfig", FormMethod.Post, new { id = "Id" }))
{
#Html.DropDownList("Cities")
}
// ON SELECTION OF ABOVE I SHOULD BE ABLE TO BIND THE BELOW GRID VIEW
#if (Model != null)
{
foreach (var item in Model)
{
#Html.DisplayFor(modelItem => item.Keywords)
#Html.ActionLink("Edit", "Edit", new { id = item.Id })
#Html.ActionLink("Delete", "Delete", new { id = item.Id })
// BELOW IS THE CODE IN CONTROLLER
public ActionResult Index()
{
var cities = So.BL.City.GetCities();
ViewBag.Cities = new SelectList(cities, "Id", "Name");
return View();
}
[HttpPost]
public ActionResult BindGridView(int id)
{
var filter = So.BL.FacebookFilter.SelectFBApprovalFilters(id);
return View(filter);
}
If you want to perform an operation on dropdown on change event you must go for javascript/jquery for handling the element events. To satisfy your requirement you may do as,
Create a <div> to hold the new grid view to be created with the id to select.
Create a partial view that generate the grid view.
Create a function either in jquery/javascript for dropdown list on change event.
From the function call an action method using ajax.
Create an action method which would do what ever the functionality you want to do and return the partial view with the model required for generating the grid view.
Finally on success method of ajax call, write the output response to the inner html of the <div>.
If definitely not going for jquery or javascript then do a post back and from the selected dropdown value in the post method fetch the data for grid view and bind it to the grid. Note here the form submission will not occur on dropdown selection must use a submit button. Also show the grid based Model if not null.
Choose which one you likely to adopt.

Html.DropdownListFor troubleshoot

I'm trying to learn the syntax for DropDownListFor.
Given the following in a for loop:
#Html.DropDownListFor(
model => model.SalutationID, Model.AvailableSalutations.Select(option => new SelectListItem
{
Selected = (option.ID == staff.SalutationID),
Text = option.Desc.ToString(),
Value = option.ID.ToString()
}
),
"Choose...")
... and given that staff.SalutationID does echo correct values (when, for example, I just use #Html.ValueFor(model => staff.SalutationID)), why does every dropdown echoed in my loop default to "Choose..." and not the Selected option?
What decides what is the selected option in the drop down list is the value of the model property specified by the lambda in the first parameter in the Html.DropDownListFor. In your case model => model.SalutationID.
Verify that model.SalutationID has the expected value in the model. If not, specify it in the controller's action, before rendering the view.