SAPUI5 get oData model from table binding - binding

I have binded a Table using oData like below.
<Table id="myTableID"
class="sapUiResponsiveMargin"
items="{Items}"
noDataText="{i18n>NoDataText}"
updateFinished="onUpdateFinished"
width="auto">
Now I need to retrieve this oData model from the view rather than calling the web service again.
I have tried the below,
var path = this.getView().getElementBinding().getPath();
var oModel = this.getView().getModel();
which returns the model from the parent view.
Is there a way to achieve this?

Just use this.getView().byId("myTableID").getModel();

I assume that you want only the items that are bound to the table.
var sPath = oTable.getBindingPath("items");
This gives you the path that you have bound to the items aggregation.
var oModelObject = oTable.getModel().getProperty(sPath);
This fetches only the table bound objects from your model
Edit oTable is your table object. var oTable = this.getView().byId("myTableID");

Related

SAPUI5 - Get binding for multiple tables with the same id

I'm trying to implement filtering on a table. This involves the getBinding() function.
getBinding() works fine with a single table - id of "stock_table".
// update list binding
var list = this.getView().byId("stock_table");
var binding = list.getBinding("items");
However, when accessing multiple generated tables of the same Id, the getBinding() returns 'undefined'
How do I apply a filter to multiple tables with the same id?
Solution was this...
Thanks for your help!
// Filter every table with the group container
var oGroupContainer = this.getView().byId('groups_container');
var oGroupItems = oGroupContainer.getRows();
_.each(oGroupItems, function (oCategory) {
var content = oCategory.getAggregation("content");
var itemBinding = content[0].getBinding("items");
itemBinding.filter(aFilters);
});

Count items in ViewResult or ActionResult - MVC Visual Studio

I need to unit test the controllers method results with the following code
StoreController test = new MvcMusicStore.Controllers.StoreController();
ActionResult result = test.Index();
ViewResult p = (ViewResult)result;
result and p shows 10 items in the model when debugging but it seems that I can not do Count or foreach to check how many records are returned as both objects are dictionary.
How can this be done?
Thanks
ViewResult.Model is of type object you have to cast it to the collection type first to get a count.
For example, if you're passing the list of integers as a model to your view, you can get the count this way:
var model = p.Model;
var list = model as List<int>();
var count = list.Count;
Assuming you have a model of IEnumerable<DataType> and you want to get this model in a unit test, you have to cast the result.ViewData.Model to your type and then do your Assertions:
var test = new StoreController();
var result = test.Index() as ViewResult;
var data = (IEnumerable<ModelType>) result.ViewData.Model;
Assert.AreEqual(10, data.Count());
Reference: https://www.asp.net/mvc/overview/older-versions-1/unit-testing/creating-unit-tests-for-asp-net-mvc-applications-cs (Listing 4)

Passing value in Partail View and retrieve

I have a partial view in mvc3 for rendering tbody content.
So how can I pass count vale in partial view and get counter value in my Serial Number column
PartialViews can have Models assigned to them just like regular views:
#model Namespace.SubNamespace.Folder.ClassName
Then, you can call Html.Partial and pass a new instance of that model:
#Html.Partial("ViewName", new ClassName() {//set values here});
If you do this, you can calculate the number of rows before the partial call and use that elsewhere:
#{
var numRows = dataSource.Rows.Count(); //use whatever count method you like
}
Meaning you can calculate the row count and set it as part of the PartialView's view model:
#{
var numRows = dataSource.Rows.Count(); //use whatever count method you like
}
#Html.Partial("ViewName", new ClassName() {RowCount = numRows});

Display value in Database DataContext

This is my Query =>
var department = dataContext.Query<Department>("SELECT Id FROM tblDepartment");
I want to display the values in variable Department in ViewBag or Anywhere.How can I do that?
var department = dataContext.Query<Department>("SELECT Id FROM tblDepartment").ToList()
and then work with list
You can use linq to Entities without executing raw queries as:
var department = dataContext.Department.Select(x=>x.Id).ToList();
If you are stubborn to use raw query, you can use as teo van kot has suggested above:
var department = dataContext.Query<Department>("SELECT Id FROM tblDepartment").ToList();
You can assign the list to ViewBag in controller as:
ViewBag.DepartmentIDs = department;
You can display the values in View as:
#{
foreach(var item in ViewBag.DepartmentIDs)
{
<span>#item <br/></span>
}
}

passing anonymous object to view

I want to pass two values from controller action to asp.net MVC 3 Razor view. I am doing like this in action method:
var model = new { reportid = rid, refno = refernumber};
return View(model );
but when i try to access it in view like this:
#Model.reportid
I get, object doesn't contain property reportid
How can I pass multiple values without using viewbag ?
Well, i strongly recommend you to use ViewModel class. But if for some reason you have phobia of extra viewmodel classes, you can use C# feature called Tuple
var model = Tuple.Create(firstObject, secondObject)
and then your view will be of type Tuple, for example
#model Tuple<int, string>
and you can access them like this
#Model.Item1
And then you can congratulate yourself, you have achieved nice mess with hidden meaning :)
Another way to accomplish this task - is to use ExpandoObject.
dynamic model = new System.Dynamic.ExpandoObject();
model.reportid = 123
model.refno = 456;
Set the view model type to dynamic:
#model dynamic
#Model.reportid
#Model.refno

Resources