I'm Working with Asp .net MVC3.I'm displaying a table in front end.I'm using a stored procedure to populate the table table columns will be populated dynamically.For example i have contains id,name,chk1,chk2,chk3 columns the stored procedure populates table where columns of the table will be populated dynamically.how can i handle this situation in view page.
following is my view ,
<table>
<tr>
#foreach (var item in Model.colName)
{
<th>#item.column_name</th>
}
</tr>
#foreach (var item in Model.bgv)
{
<tr>
<td>#item.id</td>
<td>#item.name</td>
<td>#Html.DropDownList("ddlchk", new SelectList (Model.dd, "Validation_Code", "Validation_Status","Validation_Status"))</td>
<td>#Html.DropDownList("ddlchk", new SelectList(Model.dd, "Validation_Code", "Validation_Status","Validation_Status"))</td>
<td>#Html.DropDownList("ddlchk", new SelectList(Model.dd, "Validation_Code", "Validation_Status","Validation_Status"))</td>
</tr>
}
</table>
This works correct when stored procedure return value for all(id,name,chk1,chk2,chk3) the columns.when SP returns id,name,chk1,chk2 columns it shows td with no header?how i can handile this situation?
Try this:
<thead>
<tr>
#foreach (var item in Model.colName)
{
<th>#item.column_name</th>
}
</tr>
</thead>
Related
I have 2 tables that have a many to many relation ship
Members: id, name , ...
Locations: id,location_id
Connected by a pivot table
members_location: member_id, location_id
in my Location model I have the following function
public function members(){
return $this->belongsToMany('App\Member','location_member','location_id','member_id');
}
LocationController.php Passes data to the blade template
$locations = Location::All();
return view('locations.overview',['locations' => $locations]);
So in my overview.blade.php i do the following
#include('includes.message-block')
<!-- get a list of locations -->
<table class="member_overview table table-striped table-hover">
<thead>
<tr>
<th>Naam</th>
<th>Locatie</th>
<th>Opmerking</th>
</tr>
</thead>
<tbody>
#foreach($locations as $location)
<tr>
<td>
{{$location->location_id}}
</td>
<td>
{{$location->members->id}}
</td>
<td>
</td>
</tr>
#endforeach
Which returns an error.
Undefined property: Illuminate\Database\Eloquent\Collection::$id (View: ...)
If i drop id property on:
<td>
{{$location->members->id}}
</td>
I get an array [{"id":1,"first_name":"Sa ..}]. how can I select the first_name property from that array
tinker output when selecting one location
$location->members
=> Illuminate\Database\Eloquent\Collection {#658
all: [
App\Member {#664
id: 1,
the belongsToMany relationship will return a collect not a single instance of an eloquent object.
To overcome this issue you will either have loop through the members e.g.
foreach($location->members as $member) ...
of if you just want the first one then you could do:
$location->members->first()->id
Also, just an FYI but you're going to end up with a potentially large number of DB calls due to the n+1 issue that comes with many-to-many relationships. To overcome this you can simple add a with method in your controller i.e.
$locations = Location::with('members')->get();
NB you must use get and not all (like above) if you have any other methods in the chain.
Hope this helps!
I'm using Knockout for data binding and using dataTable+YADCF for Sorting and filtering. My scenario is little complex, on clicking the Category nodes (left side) each time need to make an AJAX call and refresh the table data (right side) through Knockout. This Knockout binding functionality works fine without any issue.
HTML Code
<table class="pdm-data-table pdmList" id="ListCatAttrVal" data-link="row">
<thead>
<tr>
<th>Display Name</th>
<th>Display Source</th>
</tr>
</thead>
<tbody id="listAttribute" data-bind="foreach: attributevalue">
<tr>
<td data-bind="text: dispName"></td>
<td data-bind="text: dispSrc"></td>
</tr>
</table>
Knockout Model Code
if (!ko.dataFor(document.getElementById("listAttribute"))) {
var attributeModel = function () {
this.attributevalue = ko.observableArray();
};
attributeBinding = new attributeModel();
ko.applyBindings(attributeBinding, document.getElementById("listAttribute"));
}
Issue is after applying DataTable for the Table
$("#ListCatAttrVal").dataTable().fnClearTable();
for (var x in response.attributes) {
attributeBinding.attributevalue.push(response.attributes[x]);
}
$("#ListCatAttrVal").dataTable();
After this, Datatable Sorting is not working.
I'm trying to remove the existing generated dataTable and re-initiate it every-time when I click on the category node. But it is not working as expected.
I had a similar issue while working with knockout and datatables - my bindings inside the datatable don't seem to work initially. As a workaround I ended up initialising the datatable in the following way:
var table = $("#ListCatAttrVal").dataTable();
table.fnPageChange(0, true);
After calling fnPageChange (or any other function of datatable library, I believe) bindings seem to be working.
Iam using ASP.Net MVC3 Razor, I have two listboxes in .cshtml, let's say lbox1 and lbox2. I have binded data in the controller and sent to listboxes using viewbag. Both the listboxes have same data in it like A,B,C,D.But we can select more than one items from the listbox.. Now i want lbox1 selectionmode as single.
How can i restric it to single select ???
I have a controller where i binded the data
public Actionresult Index()
{
ViewBag.lbox1= new SelectList(db.boxes, "ID", "Name");
}
This is my view
<table>
<tr>
<td>
#Html.ListBox("lbox1")
</td>
<td>
#Html.ListBox("lbox2")
</td>
</tr>
</table>
could any one help me
Use a DropDown instead of a ListBox:
#Html.DropDown("lbox1")
I have a Model containing many rows. One of the columns in the row shows the datastore location. What I would like to do is to have a table of data for each datastore location. Is there some simple way I could do this with Razor? Here's a simplified example of what I have.
<table>
#foreach (var item in Model) {
<tr>
<td>#item.Datastore</td>
<td>#item.xxx</td>
<td>#item.yyy</td>
</tr>
}
</table>
If you haven't got the list of individual data sources available to you in your model you will have to extract them directly from you model. You can do this using Linq and using the Distinct method to find the unique values.
Once you have those you can then loop through the list and create individual table for each data store and populate them using filtered values from your model based on the data store value.
Something like this should work:
#{
var dataStores = Model.Select(i => i.DataStore).Distict();
foreach(var dataStore in dataStores) {
<table>
#foreach (var item in Model.Where(i => i.DataStore == dataStore)) {
<tr>
<td>#item.Datastore</td>
<td>#item.xxx</td>
<td>#item.yyy</td>
</tr>
}
</table>
}
}
I know this sound somewhat off-piste but how would you create a weakly typed view where you pass in a collection of objects and iterate in razor accordingly and display in a table?
-- Controller View --
???
-- Razor View ---
#foreach (var item in Model)
{
<tr>
<td>
#item.attr1
</td>
<td>
#item.attr2
</td>
</tr>
}
Frist you know the data send
From Controller -------> view by two way
By weak type view
and by strong type view
there is no other way of passing data from controller to view ...(remember)
what is intelliscence ----> which show the related sub property of any model
like we write Model. --------> then all property show in
droupdown list after dot(.).
A.what is weak type view
This is used without using model i.e like using ViewBag and other.
There is no intellisence for this type of view and it is complicated, and when you write
any name which not exist then it give at runtime error.
Ex.
.............Controller
ViewBag.List = List<job>;
return View();
.............Razor View
#foreach(var item in ViewBag.List)
{
// when you write no intellisence and you want to write your own correct one...
#item.
}
B. What strongly type view
this is used model to send data from controller to view an vice-versa.
Model are strongly typed to view so, it show intellicence and when you write wrong
then there only error show at compile time..
Ex.
.................Controller
List<job> jobdata =new List<job>();
return View(jobdata);
................view
//Mention here datatype that you want to strongly type using **#model**
#model List<job>
#foreach(var item in Model)
//this **Model** represent the model that passed from controller
// default you not change
{
#item. //then intellisence is come and no need write own ....
}
that is weak and strong type view ......
So now you solve any problem with this basic.....
may I hope it help u....
But it is best to use Strongly Typed view so it become easy
to use and best compare to weak...
#model dynamic
Will do what you want, I believe.
If its going to be a collection, then maybe use
#model ICollection
It's not weakly typed. It's typed to a collection of some kind.
#model IEnumerable<MyClass>
OK, late to the party I know, but what you're after is a view stating "#model dynamic" as already stated.
Working Example: (in mvc3 at time of posting)
NB In my case below, the view is actually being passed a System.Collections.IEnumerable
As it's weakly typed, you will not get intelesense for the items such as #item.Category etc..
#model dynamic
#using (Html.BeginForm())
{
<table class="tableRowHover">
<thead>
<tr>
<th>Row</th>
<th>Category</th>
<th>Description</th>
<th>Sales Price</th>
</tr>
</thead>
#{int counter = 1;}
#foreach (var item in Model)
{
<tbody>
<tr>
<td>
#Html.Raw(counter.ToString())
</td>
<td>
#item.Category
</td>
<td>
#item.Description
</td>
<td>
#item.Price
</td>
</tr>
#{ counter = counter +1;}
</tbody>
}
</table>
}
Edit: removed some css