I'm creating a view where the model is List of Foo. I want to create a list with an actionlink to select a given Foo item and need to pass it back to the controller. However, there is nothing unique enough in Foo to tie to the actionlink. I'd like to pass the index of the item in Foo but don't know how to get the index from the model inside of the view.
Is this possible to do this without creating a viewmodel that contains the index?
Do you mean #for(int i=0; i<Model.Count; i++), then use i as the id? But you'll need to make sure the positions won't change.
I think it will be better to have ids on Foo in the long term.
Related
My friends, Could you please explain the concept of these methods to me? Normally, when I get a data I would use 'index' to refer to items in a model and specify roles via data attribute. I came across 'item' method today and the explanation of this method is "Returns the item for the given row and column if one has been set; otherwise returns 0." What are differences from 'index'+'data' method? Is it just a shortcut?
This is the documentation of 'item' method.
https://doc.qt.io/qt-5/qstandarditemmodel.html#item
Alternatively, this is the documentation of 'index' method.
https://doc.qt.io/qt-5/qstandarditemmodel.html#index
I'll try my best to explain it.
The item is like the actual widget you see in the view (it's not actually a widget, but I think it's a good way to think about it). It's what the user is actually seeing.
The index is more "behind the scenes." It's like a pointer to a position in the model.
An item can exist without an index. But a valid index cannot exist without an item. The item only associates with an index when it is put into a model. Otherwise, it's just an item that no one can look at.
Take an array as an example... It contains multiple "items". You specify which item from the array you want by providing a number, aka the index. Simply put, the index only exists when it is associated with an item in the array. But the item can exist outside of the array and be it's own thing without an index.
The QModelIndex was created to be a lightweight way to reference items in a model. Similar to the way you can use a number to represent an object stored in an array.
I want to use model values on view page but in shortcode way?
For Example:
#model.name prints name which is assigned to model by controller.
But I want to use it with out #model.name by another token or shortcode?
For example now we use #model.Name for print its value on view page but I want that value with out using #model.Name.
I want to use another token which can print the same value of it.
I have found a solution; I can use tuple with view bag.
A view bag is a dynamic object using session, so make a tuple and then pass it to view.
I have a view that contains 4 columns (HTML Divs). Each contains a different subset of the same data, based on my Place model (I'm just outputting the name property, there is also a category property - both strings).
I could create a ViewModel, containing 4 place models, each with the appropriate data in.
I could pass 4 strings with markup in (probably not good as mixing markup with data?).
What would be the best practice here?
(I also need to output the places in alphabetical order, by category - outputting a heading when the category changes - though this is a secondary requirement).
You should probably use the first option, a ViewModel containing 4 PlaceViewModels. It will provide you with the flexibility of manipulating the objects and their properties, and display them as you want; and it will also improve maintenance of the view in the long run,
#model PlacesViewModel
#foreach (var place in Model.Places)
{
// Render your HTML column here
}
By passing a set of HTML strings (as mention in the second option) you won't be able to modify, inspect, parse, iterate or manipulate your objects in any useful way. You would be left to manipulate the strings using some sort of client-side framework, like jQuery.
However, if you simply want to iterate over a set of PlaceViewModel, you think about passing a list of them, and in your View do something like,
#model List<PlaceViewModel>
#foreach (var place in Model)
{
// Render your HTML column here
}
which will save the need of having an extra class.
To order the models alphabetically by name (or any other property), do,
#foreach (var place in Model.Places.OrderBy(place => place.Name))
{
// Render your HTML column here
}
I have a Dev-Ex Tree List which has two columns, List contains elements inside it, Now My question is if i want to add any new item in the list then logic should search existing items in the tree, if no match found then it should allow to add that item in the list,otherwise not.
can i make a method which keep on checking recursively new item with the other item in the list.
Such tasks are usually solved by using TreeList Iterator. I think that the How to Implement an Iterator for the XtraTreeList (FindNode Example) knowledge base article contains the code you are looking for.
Is there any way to get the sum of items based on some filters from sharepoint list? I was trying to access the GetListItems method. But this returns all the items in the list. That makes the data heavy. My requirement is to get only the sum of items.
For example items created in a specific year. I am trying to populate a chart in flex from the sharepoint list. Accessing all the items and then calculating the sum in flex will not work always where the list contains more items.
Use the viewFields parameter of GetListItems to only return a single (small) field like ID. It is still data heavy but better than returning all the fields from the list.
There is a rowLimit parameter too that you can use to return all the list items instead of only a block at a time.
Info on the GetListItems method with code snippets at MS:
http://msdn.microsoft.com/en-us/library/lists.lists.getlistitems(v=office.12).aspx