what is the best way to use Html.DropDownListFor when you have a list of objects? - asp.net-mvc-2

In my view model if have a:
List<Car>
where car has an Id and a name. I want to create a dropdown box using
Html.DropDownListFor()
what is the best way to hook this up as i want to have the value of the item be the Id and the display to be the Name of the Car.

What's in your view model,
to display the list you want you would use
<%= Html.DropDownList("DropDownName", new SelectList(yourListOfCar, "Id", "Name"))%>
so if you want to use DropDownListFor, you would use is like this
<%= Html.DropDownList(model => model.IdCar, new SelectList(yourListOfCar, "Id", "UserName"))%>
where model is your view model

Related

How to filter with NSPredicate by an attribute of an Element I pass from the parent view?

Please help me with the following question - how do I filter a fetchRequest by an attribute of an Element I pass from the parent view?
import SwiftUI
struct aListDetailView: View {
#State var aParameter: String
#FetchRequest(sortDescriptors: [], predicate: NSPredicate(format: "elementIdentificationAttribute == %#", aParameter)) var contracts: FetchedResults<Contract>
var body: some View {
List {...}
...}
I get "Cannot use instance member 'aContract' within property initializer; property initializers run before 'self' is available".
I understand why, but how do I bypass this?
My example is a simplified version.
In truth, my query is this: I have a list of Contract elements, and each contract has a list of invoices issued in regards to that specific contract. Each contract has a unique serial code I give it, as an attribute.
My #FetchRequest for a list of all Contract elements is in Content View. I loop through them to create a list. Each row of the list is a NavigationLink to a new view, and I pass the specific contract element to the new view.
In the new view, I need to fetch a list of all the invoices specific to that contract.
However, when I add a new invoice to create a new row in the invoice sub-view list, core data does not show the new row, for a new invoice. The new row appears only when I return to the Content View and back to the invoice view, because my #FetchRequest for Contracts is in Content View.
I thought I could bypass this issue through a #FetchRequest for a list of contracts that have an attribute of my choosing (say, an attribute contractSerialNumber) equal to the attribute instance of the contract I pass to this view.
Something like:
#FetchRequest that filters all contracts in storage by the specific, unique attribute of my choosing of the contract I passed from the Parent View. Places them in a list.
ForEach contract that has that particular attribute (which will be a list of one contract) {
ForEach contract.invoiceArray { invoice in
Text(contract.invoice.invoiceIdentificationAttribute
} }
Thank you!

Go, Mongo problems

I am new using Go, but I would like to know if there is a problem if I add new attributes to a Collection in Mongo, once my model is defined in Go. Form example
I defined in Go this Model:
type material struct {
ID string `bson:"_id" json:"_id"`
Name string `bson:"name" json:"name"`
Entity string `bson:"entity" json:"entity"`
}
and I create new attributes on Material Collection for example:
Collection Material
All Attributes: ID, Name, Entity,Country(NEW ONE)
Is it necessary to update the model knowing that I will not use that attribute for the project?

how to show list of CustomerId in dropdown list

i want to create Customer diary on the base of customerID that present in customer table.
In customerID field i want dropdown list having list of CustomerId's that are present in database
How can i do that ? can someone help me please
var _objAllCustomerIds = null;
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
_objAllCustomerIds = context.MyCustomers.Select(customer => customer.Id).ToList();
}
//where AdventureWorksEntities is your DBcontent containing all your entities
//MyCustomers is the entities representation of your customer table
if this is in your .aspx file
then in your .aspx.cs file (assuming you're using code behind) you'd get the list of customer IDs and then bind that list to this dropdown.
Does this answer from another question help?
How to bind the selected value of a DropDownList

link multiple models on same row of sap.m.table

This may be a basic question, but it's my first, so please be kind :-).
I have a sap.m.table with two models, one model with transaction data (trxModel) and another model that is used to display a sap.m.select list (reasonCodeModel). The table model is set to trxModel.
The selected value key from the dropdown needs to update a value (ReasonCodeID) in the trxModel when a value from the reason code list is selected.
I can retrieve the selected key in the change event as so
var selKey = evt.getParameter("selectedItem").getKey();
Is there a simple way to find the trxModel relevant model path from the table row Select list value I've just modified? Or is it possible to bind the ReasonCodeID from the trxModel to the ReasonCodeID field in the reasonCodeModel?
Just an extra piece of info, The current row is selected and is accessible
var selItem = dtlTable.getSelectedItem();
2nd question and I guess could be kind of related, is there a way of getting the table model path based on the selected item (highlighted row) of the table? And vice a versa?
More details on Select & Table binding.
var tabTemplate = new sap.m.ColumnListItem(
{
::
new sap.m.Select(
"idReasonCodeSelect",
{
enabled : false,
change : function(evt) {
oS4View.getController().changeReasonCodeSel(evt);
}
}
),
Bind the resource code Select to the Table
// bind the reason codes to the reason code model
sap.ui.getCore().byId("idReasonCodeSelect").setModel(
oReasonCodeModel);
sap.ui.getCore().byId("idReasonCodeSelect").bindAggregation("items", "/results",
new sap.ui.core.Item({
key : "{ReasCodeID}",
text : "{ReasCodeDesc}"
}));
Per Qualiture comment, how do I bind the Select key to the table model ReasonCodeID value?
I found an approach to tackle the first part of my question above
From the change function on the Select, I can find the path of the table model using the following.
var path = evt.getSource().getParent().getBindingContext().sPath;
2nd Update:
On the selectionChange event on the table, there's a couple of options to find the associated model path or model content.
// find the model path
oModelPath = selItem.getBindingContext().getPath();
// model values
oItem = oEvent.getParameter("listItem").getBindingContext().getObject();
So my only remaining issue, While I loop through the table model results (trxModel) and I want the Select List (using setSelectedKey) to reflect the ReasonCodeID value in the trxModel.

Two model to a controller in sapui5

I have json model called contact this is how it looks:
{firstName:"",lastName:"",country:""}
I have an another model called country which contains the list of countries. I want this list of countries in a dropdown. While selecting a country from the dropdown, country field in contact model should get updated. How can i achieve that?
The simple steps to get this done are:
1. Set the Contact Model where ever you want such as View or Core.
2. Set the Country Model to DropDown list.
3. On selection of the DropDown list, on Change function you set Country field by the value you select in Dropdown list.
4. This will update the country value in the Contact Model.
you can use named data model and multimodel support
In your controller js,
this.getView().setModel(oCountryModel,"Country");
this.getView().setModel(ocontactModel ,"Contact");
If you want set Contact model with your default value, add logic in your init function:
init:function() {
//initialize contact model with default country
var oFilter = new sap.ui.model.Filter("country",
sap.ui.model.FilterOperator.EQ ,"America");
//suppose you have a Contact table called ContactList
var oTable =this.getView().byId("ContactList");
oTable.getBinding("items").filter([oFilter]);
}
In your view xml, use named model data binding, for example:
<Text text="{Contact>/firstName}"/>
Then you are using two models in your controller and view xml. Regarding dropdown selection change triggers an update for your contact, you need to attach event listener to the dropdown, and update the Contact data model based on the selected country. See the following code:
handleSelectionChange:function(oEvent) {
//get the new selected country
var country = oEvent.getParameters().newValue;
var oFilter = new sap.ui.model.Filter("country",
sap.ui.model.FilterOperator.EQ ,country);
//suppose you have a Contact table called ContactList
var oTable =this.getView().byId("ContactList");
oTable.getBinding("items").filter([oFilter]);
}