Rerender View with condition - asp.net-mvc

There is a following View:
<div class="table-responsive hidden" data-bind="css:{hidden : dataSource().length <= 0}, template: {afterRender: myPostProcessingLogic}">
<table class="table table-bordered table-striped table-condensed">
<thead>
<tr>
<th class="text-center">#AdminResource.Admin_Report_Location</th>
<th class="text-center">#AdminResource.Admin_Report_Employee</th>
<th class="text-center">#AdminResource.Admin_Report_TicketNumber</th>
<th class="text-center">#AdminResource.Admin_Report_Date</th>
<th class="text-center">#AdminResource.Admin_Report_Customer</th>
<th class="text-center">#AdminResource.Admin_Report_SubTotal</th>
<th class="text-center">#AdminResource.Admin_Report_Tax</th>
<th class="text-center">#AdminResource.Admin_Report_Tips</th>
<th class="text-center">#AdminResource.Admin_Report_Discount</th>
<th class="text-center">#AdminResource.Admin_Report_SalesPreTax</th>
<th class="text-center">#AdminResource.Admin_Report_SalesTotal</th>
<th class="text-center">#AdminResource.Admin_Report_Paid</th>
<th class="text-center">#AdminResource.Admin_Report_PaidPreTax</th>
<th class="text-center">#AdminResource.Admin_Report_PaymentMethod</th>
#if ((bool?)ViewBag.WithCreditCard != null)
{
if ((bool)ViewBag.WithCreditCard)
{
#:<th class="text-center">#AdminResource.Admin_Ticket_CreditCardLast4Digits</th>
#:<th class="text-center">#AdminResource.Admin_Report_PaymentMethod</th>
}
}
<th class="text-center"></th>
</tr>
</thead>
<tbody data-bind="foreach: pagedDataSource">
<tr>
<td data-bind="text: Location"></td>
<td data-bind="text: Employee"></td>
<td data-bind="text: CustomReceiptNumber"></td>
<td data-bind="textAsDateTime: UserOrderDate"></td>
<td data-bind="text: CustomerFullName"></td>
<td data-bind="textAsCurrency: SubTotal" class="amount-formated"></td>
<td data-bind="textAsCurrency: Tax" class="amount-formated"></td>
<td data-bind="textAsCurrency: Tips" class="amount-formated"></td>
<td data-bind="textAsCurrency: Discount" class="amount-formated"></td>
<td data-bind="textAsCurrency: SalesPreTax" class="amount-formated"></td>
<td data-bind="textAsCurrency: OrderTotal" class="amount-formated"></td>
<td data-bind="textAsCurrency: OrderPaid" class="amount-formated"></td>
<td data-bind="textAsCurrency: PaidPreTax" class="amount-formated"></td>
<td data-bind="text: PaymentMethod"></td>
#if ((bool?)ViewBag.WithCreditCard != null)
{
if ((bool)ViewBag.WithCreditCard)
{
#:<td data-bind="text: CreditCardLast4"></td>
#:<td data-bind="text: AuthorizationTransactionCode"></td>
}
}
<td>
<button type="button" class="btn btn-circle btn-bordered btn-inverse btn-to-primary" data-bind="click: $parent.previewTicket"><i style="margin: 0" class="fa fa-eye"></i></button>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td><strong data-bind="visible: Count">#AdminResource.Common_Total: <span data-bind="text: Count"></span></strong></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td><strong data-bind="textAsCurrency: SubTotalSum" class="amount-formated"></strong></td>
<td><strong data-bind="textAsCurrency: TaxSum" class="amount-formated"></strong></td>
<td><strong data-bind="textAsCurrency: TipsSum" class="amount-formated"></strong></td>
<td><strong data-bind="textAsCurrency: DiscountSum" class="amount-formated"></strong></td>
<td><strong data-bind="textAsCurrency: OrderPreTaxSum" class="amo
unt-formated"></strong></td>
<td><strong data-bind="textAsCurrency: OrderTotalSum" class="amount-formated"></strong></td>
<td><strong data-bind="textAsCurrency: OrderPaidSum" class="amount-formated"></strong></td>
<td><strong data-bind="textAsCurrency: PaidPreTaxSum" class="amount-formated"></strong></td>
<td></td>
<td></td>
</tfoot>
</table>
#RenderPage("~/Views/Shared/Partial/_PagedDataSourceControl.cshtml")
It is used as a partial in another View:
#{
ViewBag.Title = AdminResource.Admin_Navigation_SalesByPaymentMethodTypeReport;
Layout = "~/Views/Shared/_LayoutMaterial.cshtml";
}
#section ReportHeader{
#Html.MaterialReportHeader((string)ViewBag.Title, (bool)#ViewBag.IsFavorite)
}
<div class="row">
<div class="col-md-6">
#if (ViewBag.IsReportsByRevenueDate)
{
#Html.QuickDateTimes(null, null, "MM/dd/yyyy hh:mm tt")
}
else
{
#Html.QuickDates(null, null)
}
</div>
<div class="col-md-4">
<div class="form-group bmd-form-group m-b-20">
#Html.LabelFor(m => m.SelectedCompanies, new { #class = "bmd-label-static" })
#if (ViewBag.LocationsDropDownTreeItems.Count > 0)
{
#Html.BootstrapDropDownTreeViewList((List<IConnect.Domain.Reports.DropDownTreeItem>)ViewBag.LocationsDropDownTreeItems);
}
else
{
<select class="form-control" data-bind="kendoMultiSelect: { data: locationsDTO, dataTextField: 'Name', dataValueField: 'Id', value: selectedLocations, placeholder: 'All' }"></select>
}
</div>
<div class="form-group bmd-form-group m-b-20">
#Html.LabelFor(m => m.SelectedEmployees, new { #class = "bmd-label-static" })
<select class="form-control" id="ddlEmployees" data-bind="kendoMultiSelect: { data: employeesDTO, dataTextField: 'Name', dataValueField: 'Id', value: selectedEmployees, placeholder: '#AdminResource.Common_All', open: onEmployeesDDLOpening }"></select>
</div>
<div class="form-group bmd-form-group m-b-20">
#Html.LabelFor(m => m.SelectedPaymentMethods, new { #class = "bmd-label-static" })
<select class="form-control" data-bind="kendoMultiSelect: { data: paymentMethodsDTO, dataTextField: 'Name', dataValueField: 'Id', value: selectedPaymentMethods, placeholder: '#AdminResource.Common_All' }"></select>
</div>
</div>
</div>
**<div class="horizontal-scroll" , id="partdetail">
#RenderPage("Partial/_Details.cshtml")
</div>**
I need to implement the display conditions of the two columns depending on the Viewbag.WithCreditCard value which depend on the selected selected PaymentMethods. That is, redraw the first view when the selectedPaymentMethods () value changes. Implemented subscribe:
self.selectedPaymentMethods.subscribe(function () {
debugger;
var cyclenumber = 0;
if (self.selectedPaymentMethods().length == 0)
cyclenumber = self.selectedPaymentMethods().length;
else
cyclenumber = self.paymentMethodsDTO().length;
console.log(self.paymentMethodsDTO().length);
console.log(self.selectedPaymentMethods().length);
for (var i = 0; i < cyclenumber; i++) {
if (self.paymentMethodsDTO()[i] == 5){
#{
ViewBag.WithCreditCard = true;
}
}
else
if (self.selectedPaymentMethods()[i] == 5){
#{
ViewBag.WithCreditCard = true;
}
}
}
});
paymentMethodsDTO - observable array where all types of payments.
I need to figure out how to redraw the partial view when changing selectedPaymentMethods

Part of the problem that you will need to overcome is the bringing the payment selection client side and allowing the user to select the payment method then allowing knockout to do its thing. the crudest and probably the easiest way to hide and show columns can be done by applying knockout if and ifnot bindings to the column that you want to hide and show.
var ViewModel = function() {
var self = this;
self.availablePaymentTypes = ko.observableArray(['Cash','CreditCard', 'EFT', 'Cheque']);
self.dataSource = ko.observableArray([]);
self.myPostProcessingLogic = function() {
alert('post processing');
};
self.paymentType = ko.observable('CreditCard');
self.pagedDataSource = ko.observableArray([]);
self.previewTicket = function(data) {
alert('preview Ticket');
}
self.count = ko.pureComputed(function() {
return self.pagedDataSource().length;
});
self.SubTotalSum = ko.pureComputed(function() {
return 0;
});
self.TaxSum = ko.pureComputed(function() {
return 0;
});
self.TipsSum = ko.pureComputed(function() {
return 0;
});
self.DiscountSum = ko.pureComputed(function() {
return 0;
});
self.OrderPreTaxSum = ko.pureComputed(function() {
return 0;
});
self.OrderTotalSum = ko.pureComputed(function() {
return 0;
});
self.OrderPaidSum = ko.pureComputed(function() {
return 0;
});
self.PaidPreTaxSum = ko.pureComputed(function() {
return 0;
});
};
var vm = new ViewModel();
ko.applyBindings(vm);
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<select class="form-control" data-bind="options: availablePaymentTypes, value: paymentType" ></select>
<div class="table-responsive hidden" data-bind="css:{hidden : dataSource().length <= 0}, template: {afterRender: myPostProcessingLogic}">
<table class="table table-bordered table-striped table-condensed">
<thead>
<tr>
<th class="text-center">#AdminResource.Admin_Report_Location</th>
<th class="text-center">#AdminResource.Admin_Report_Employee</th>
<th class="text-center">#AdminResource.Admin_Report_TicketNumber</th>
<th class="text-center">#AdminResource.Admin_Report_Date</th>
<th class="text-center">#AdminResource.Admin_Report_Customer</th>
<th class="text-center">#AdminResource.Admin_Report_SubTotal</th>
<th class="text-center">#AdminResource.Admin_Report_Tax</th>
<th class="text-center">#AdminResource.Admin_Report_Tips</th>
<th class="text-center">#AdminResource.Admin_Report_Discount</th>
<th class="text-center">#AdminResource.Admin_Report_SalesPreTax</th>
<th class="text-center">#AdminResource.Admin_Report_SalesTotal</th>
<th class="text-center">#AdminResource.Admin_Report_Paid</th>
<th class="text-center">#AdminResource.Admin_Report_PaidPreTax</th>
<th class="text-center">#AdminResource.Admin_Report_PaymentMethod</th>
<!-- ko if: paymentType() == 'CreditCard' -->
<th class="text-center">#AdminResource.Admin_Ticket_CreditCardLast4Digits</th>
<!-- /ko -->
<!-- ko ifnot: paymentType() == 'CreditCard' -->
<th class="text-center">#AdminResource.Admin_Report_PaymentMethod</th>
<!-- /ko -->
<th class="text-center"></th>
</tr>
</thead>
<tbody data-bind="foreach: pagedDataSource">
<tr>
<td data-bind="text: Location"></td>
<td data-bind="text: Employee"></td>
<td data-bind="text: CustomReceiptNumber"></td>
<td data-bind="text: UserOrderDate"></td>
<td data-bind="text: CustomerFullName"></td>
<td data-bind="text: SubTotal" class="amount-formated"></td>
<td data-bind="text: Tax" class="amount-formated"></td>
<td data-bind="text: Tips" class="amount-formated"></td>
<td data-bind="text: Discount" class="amount-formated"></td>
<td data-bind="text: SalesPreTax" class="amount-formated"></td>
<td data-bind="text: OrderTotal" class="amount-formated"></td>
<td data-bind="text: OrderPaid" class="amount-formated"></td>
<td data-bind="text: PaidPreTax" class="amount-formated"></td>
<td data-bind="text: PaymentMethod"></td>
<!-- ko if: $parent.paymentType() == 'CreditCard' -->
<td data-bind="text: CreditCardLast4"></td>
<!-- /ko -->
<!-- ko ifnot: $parent.paymentType() == 'CreditCard' -->
<td data-bind="text: AuthorizationTransactionCode"></td>
<!-- /ko -->
<td>
<button type="button" class="btn btn-circle btn-bordered btn-inverse btn-to-primary" data-bind="click: $parent.previewTicket"><i style="margin: 0" class="fa fa-eye"></i></button>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td><strong data-bind="visible: count">#AdminResource.Common_Total: <span data-bind="text: count"></span></strong></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td><strong data-bind="text: SubTotalSum" class="amount-formated"></strong></td>
<td><strong data-bind="text: TaxSum" class="amount-formated"></strong></td>
<td><strong data-bind="text: TipsSum" class="amount-formated"></strong></td>
<td><strong data-bind="text: DiscountSum" class="amount-formated"></strong></td>
<td><strong data-bind="text: OrderPreTaxSum" class="amount-formated"></strong></td>
<td><strong data-bind="text: OrderTotalSum" class="amount-formated"></strong></td>
<td><strong data-bind="text: OrderPaidSum" class="amount-formated"></strong></td>
<td><strong data-bind="text: PaidPreTaxSum" class="amount-formated"></strong></td>
<td></td>
<td></td>
<td></td>
</tfoot>
</table>

Related

Table dropdown with bootstrap 5 not working

I am trying to show product items when button order detail is pressed, but its not working in any way I tried, it's possible to make it work with bootstrap 5 or I have to do it with javascript ?
Useless information for "It looks like your post is mostly code; please add some more details." : In the first foreach I am displaying information from the order, then the second foreach is retrieving product id and quantity from a different table that contain order id and the third foreach is getting product where id from the second foreach in order to display product details
<table class="table border">
<thead>
<tr>
<th class="h4" style="width: 18%">Order Number</th>
<th class="h4">Date</th>
<th class="h4">Total</th>
<th class="h4">Status</th>
<th class="h4"></th>
<td></td>
</tr>
</thead>
<tbody>
<tr>
<?php
foreach ($order as $row_order) {
$order_status = intval($row_order->status);
?>
<td class="fw-bold">{{ $row_order->id }}</td>
<td class="fw-bold">{{ $row_order->added_on }}</td>
<td class="fw-bold">{{ $row_order->order_total }} $</td>
<td class="fw-bold"><?php if($order_status == 0){ ?>
Not Paid
<?php }elseif ($order_status == 1) { ?>
Paid
<?php }elseif ($order_status == 2) { ?>
Shipped
<?php }elseif ($order_status == 3) { ?>
Delivered
<?php } ?>
</td>
<td></td>
{{-- <td><a class="btn btn-primary" href="">Order Detail</a></td> --}}
<div class="btn-group">
<td>
<button class="btn btn-primary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-expanded="false">
Order Detail
</button>
</td>
#foreach (\App\Models\Order_product_size_color::where('order_id', '=', $row_order->id)->get() as $order_variant)
#foreach (\App\Models\Product_size_color::where('id', '=', $order_variant->product_size_color_id)->get() as $variant)
<div class="dropdown-menu">
<td class="border-0 p-0 dropdown-item">
<tr>
<td class="border-0"><img class="rounded" src="{{ asset('storage/'. $variant->image1->product_image) }}" alt="" style="width: 80px; height: 90px;"></td>
<td class="fw-bold border-0">{{ $variant->product->product_name }}</td>
<td class="border-0">{{ $order_variant->count }} products</td>
<td class="fw-bold border-0">Size: {{ $variant->size->size_name }}</td>
<td class="fw-bold border-0">Color: {{ $variant->color->color_name }}</td>
<td class="fw-bold border-0">${{ $variant->product->price }}</td>
</tr>
</td>
</div>
#endforeach
#endforeach
</div>
<tr>
<td class="fw-bold">Delivery Address:</td>
<td class="fw-bold">{{ $row_order->address }}</td>
</tr>
</tr>
<?php }//end foreach ?>
</tbody>
</table>

How to implement bootstrap accordion in table in MVC?

I want to implement bootstrap accordion in my application. Currently, If I click on any rows, it gives me first #Html.Raw(item.D) and #Html.Raw(item.B).
What i want is that, if i click on 2nd row then its respective #Html.Raw(item.D) and #Html.Raw(item.B) should displayed and so on.
Please suggest me where I am going wrong.
<div class="panel-body">
<table class="table">
#foreach (var item in Model)
{
<tr class="accordion-toggle" data-toggle="collapse" data-target="#12345-cores">
<td align="left">
#Html.Raw(item.H)
</td>
<td align="right">
#Html.Raw(item.E)
</td>
</tr>
<tr>
<td class="accordion-body collapse" id="12345-cores">
<table>
<tbody>
<tr>
<td>
#Html.Raw(item.D)
</td>
<td></td>
<td>
#Html.Raw(item.B)
</td>
</tr>
</tbody>
</table>
</td>
</tr>
}
</table>
You need to specify id uniquely, to do so you need to append raw id(or something unique for each row) to id and data-target attributes,
<div class="panel-body">
<table class="table">
#foreach (var item in Model)
{
<tr class="accordion-toggle" data-toggle="collapse" data-target="#12345-cores#(item.id)">
<td align="left">
#Html.Raw(item.H)
</td>
<td align="right">
#Html.Raw(item.E)
</td>
</tr>
<tr>
<td class="accordion-body collapse" id="12345-cores#(item.id)">
<table>
<tbody>
<tr>
<td>
#Html.Raw(item.D)
</td>
<td></td>
<td>
#Html.Raw(item.B)
</td>
</tr>
</tbody>
</table>
</td>
</tr>
}
</table>
updated : data-target="#12345-cores#(item.id) & id="12345-cores#(item.id)"

keep the checkbox checked browsing through the paging (Using PagedList asp.net mvc)

I have a list of orders in a gridview with customer html which has paging using PagedList asp.net mvc. I have a checkbox for each order in the list. When I use "Check all" to check all orders it should keep the checkbox checked when I go to page 2-3-4. But I lose the checked status when I move to next page. Can anyone help please?
JavaScript code :
$(document).ready(
function () {
$('#selectAll').click(
function () {
$('#TabOrdre td input:checkbox').prop('checked', $(this).is(':checked'));
if ($(this).is(':checked')) {
$('#TabOrdre td input:checkbox').each(function () {
if ($.inArray($(this).val(), list) === -1)
list.push($(this).val());
});
getAll = true;
}
else {
list = [];
getAll = false;
}
}
);
View code :
<section class="panel panel-default">
<header class="panel-heading m-b-none">Liste des ordres</header>
<div class="row wrapper">
<div class="col-md-12 space20">
<div class="doc-buttons">
<a class="btn btn-s-md btn-warning ajout-container"> <i class="fa fa-plus"></i> Ajouter un ordre </a>
</div>
</div>
</div>
<div class="table-responsive">
<table class="table table-striped b-t b-light">
<thead>
<tr>
<th width="20">
<input type="checkbox" id="selectAll" value="-1">
</th>
<th style="white-space:nowrap">
EDA
</th>
<th style="white-space:nowrap">
<span class="th-sort">
Immediat
</span>
</th>
<th style="white-space:nowrap">
JNA
</th>
<th style="white-space:nowrap">
Date
</th>
<th style="white-space:nowrap">
Heure début
</th>
<th style="white-space:nowrap">
Heure fin
</th>
<th style="white-space:nowrap">
Puissance
</th>
<th style="white-space:nowrap">
RP
</th>
<th style="white-space:nowrap">
RS
</th>
<th style="white-space:nowrap">
Caractérisation
</th>
<th style="white-space:nowrap">
Commentaire
</th>
<th style="white-space:nowrap">
Source
</th>
<th style="white-space:nowrap">
Date de création
</th>
<th style="white-space:nowrap">
Refus
</th>
<th style="white-space:nowrap" class="th-sortable" data-toggle="class">
</th>
</tr>
</thead>
<tbody>
#if (Model.Count > 0)
{
foreach (var item in Model)
{
#Html.HiddenFor(model => item.ORD_ID)
<tr>
<td>
<input type="checkbox" value="#item.ORD_ID" id="chk_#item.ORD_ID" />
</td>
<td>
#Html.DisplayFor(model => item.EDA_NOM)
</td>
<td>
#(Convert.ToBoolean(item.EDA_IMMEDIAT) ? "Oui" : "Non")
</td>
<td>
#(Convert.ToBoolean(item.EDA_JNA) ? "Oui" : "Non")
</td>
<td>
#if (item.EDA_ORDRE_DATE_DEBUT != null)
{#Html.DisplayFor(model => item.EDA_ORDRE_DATE_DEBUT, "ShortDateTime")}
</td>
<td style="white-space:nowrap">
#if (item.EDA_ORDRE_DATE_DEBUT != null)
{#Html.DisplayFor(model => item.EDA_ORDRE_DATE_DEBUT, "ShortTimeString")}
</td>
<td>
#if (item.EDA_ORDRE_DATE_FIN != null)
{ #Html.DisplayFor(model => item.EDA_ORDRE_DATE_FIN, "ShortTimeString")}
</td>
<td>
#Html.DisplayFor(model => item.EDA_PUISSANCE)
</td>
<td>
#Html.DisplayFor(model => item.EDA_PUISSANCE_RP)
</td>
<td>
#Html.DisplayFor(model => item.EDA_PUISSANCE_RS)
</td>
<td>
#Html.DisplayFor(model => item.Eda_Caracterisation)
</td>
<td>
#Html.DisplayFor(model => item.COMMENTAIRE)
</td>
<td>
#Html.DisplayFor(model => item.SOURCE_NOM)
</td>
<td>
#if (item.EDA_DATE_RECUPEREE != null)
{ #Html.DisplayFor(model => item.EDA_DATE_RECUPEREE, "ShortDateTime")}
</td>
<td>
#Html.DisplayFor(model => item.VAL_REFUS)
</td>
<td >
#if (item.MODIFIABLE == 0)
{
<a class="btn btn-warning editor-container" data-id="#item.ORD_ID" data-toggle="modal"
><i class="fa fa-edit"></i></a>
}
</td>
</tr>
}
}
else
{
<tr>
<td class="text-center " colspan="16">
#VideResult
</td>
</tr>
}
</tbody>
</table>
</div>
<footer class="panel-footer">
<div class="row">
<div class="col-sm-4 ">
<button type="submit" class="btn btn-s-md btn-warning" id="Accepter">Accepter</button>
<button class="btn btn-s-md btn-default" id="Refuser">Refuser</button>
</div>
<div class="col-sm-4 text-center">
</div>
<div class="col-sm-4 text-right text-center-xs">
#Html.PagedListPager(Model, Page_No => Url.Action("Liste",
new
{
eda = ViewBag.eda,
Source = ViewBag.Source
,
datedeb = ViewBag.datedeb
,
datefin = ViewBag.datefin
,
refus = ViewBag.refus
,
page = Page_No
}),
PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(PagedListRenderOptions.ClassicPlusFirstAndLast
, new AjaxOptions()
{
HttpMethod = "GET",
UpdateTargetId = "TabOrdre",
LoadingElementId = "loader"
}))
</div>
</div>
</footer>
</section>

Knockout with class scoped properties

I am using Knockout and have the ViewModel bound to my data object in my ASP.Net MVC 4 project quite nicely like so:
$(document).ready(function() {
properties = #Html.Raw(Json.Encode(Model));
selectedProperty = properties[0];
viewModel = { properties: ko.mapping.fromJS(#Html.Raw(Json.Encode(Model))), selectedProperty: ko.observable()};
viewModel.setItem = function(item) {
viewModel.selectedProperty(item);
}
ko.applyBindings(viewModel);
Now I want to refactor my JavaScript so that the logic is encapsulated inside a class:
RealEstate.Search = function (properties) {
this.properties = properties;
this.selectedProperty = this.properties[0];
this.viewModel = { properties: ko.mapping.fromJS(this.properties), selectedProperty: ko.observable()};
this.viewModel.setItem = function(item) {
viewModel.selectedProperty(item);
}
ko.applyBindings(this.viewModel);
}
And I am instantiating that object in my HTML page like so:
$(document).ready(function() {
search = new RealEstate.Search(#Html.Raw(Json.Encode(Model)));
}
Now, I am getting the following error:
Error: Unable to parse bindings.
Message: ReferenceError: 'properties' is undefined;
Bindings value: foreach: properties
Here is the snipped HTML for the table bound to the ViewModel:
<div id="divDataTable" data-bind="with: properties">
<table id="dataTable" class="tablesorter">
<thead>
<tr>
<th>Address
</th>
<th>
Suburb
</th>
<th>Price
</th>
<th>Beds
</th>
<th>Baths
</th>
<th>Days Listed
</th>
</tr>
</thead>
<tbody data-bind="foreach: properties">
<tr data-bind="click: $root.setItem">
<td>
<label data-bind="text: $data.Street"></label>
<input data-bind="attr: { value : $index(), id : $index(), name : $index() }" type="hidden" />
</td>
<td data-bind="text: $data.Suburb"></td>
<td data-bind="text: $data.PriceFormatted"></td>
<td data-bind="text: $data.NumOfBedrooms"></td>
<td data-bind="text: $data.NumOfBathrooms"></td>
<td data-bind="text: $data.DaysListed"></td>
</tr>
</tbody>
</table>
</div>
</section>
<div id="divProperty">
<aside class="float-right" data-bind="with: selectedProperty">
<table>
<tr>
<td>
<label data-bind="text: $data.Street"></label>
</td>
<td>
<label data-bind="text: $data.PriceFormatted"></label>
</td>
</tr>
<tr>
<td colspan="2">
<img src="#" /></td>
</tr>
<tr>
<td>Beds:
<label data-bind="text: $data.NumOfBedrooms"></label>
</td>
<td>On OZMite:
<label data-bind="text: $data.DaysListed"></label>
</td>
</tr>
<tr>
<td>Baths:
<label data-bind="text: $data.NumOfBathrooms"></label>
</td>
<td>Year built:</td>
</tr>
</table>
</aside>
I would appreciate it if someone could shed some light on what I am doing wrong.
With the data-bind="with: properties" you are already "in the context" of the properties property inside your div.
So when you write <tbody data-bind="foreach: properties"> KO tries to find the properties property inside your properties array.
What you need is to use to reference the current binding context with the $data.
So your foreach should look like this:
<tbody data-bind="foreach: $data">
...
</todby>

MVC multiple forms on single view not working

I have 2 forms on a single razor view that open in seperate dialog boxes. The forms are submitted using jquery post..
First form works perfectly but the second form isnt recognised at all and when I try to serializr the data it returns an empty string.
Code below:
#using (Html.BeginForm("SaveElectricReading", "Store", FormMethod.Post, new { id = "frmSaveElectricReading" }))
{
<div id="electricDialog" style="display: none;" title="Electric Readings">
<p>
Please ensure the electric readings have been entered!
</p>
<table width="100%">
<tr>
<th>
Serial:
</th>
</tr>
<tr>
<td>
<input type="text" name="Serial" />
</td>
</tr>
<tr>
<th>
Reading:
</th>
</tr>
<tr>
<td>
<input type="text" name="Reading" />
</td>
</tr>
<tr>
<th>
Comment:
</th>
</tr>
<tr>
<td>
<div class="textwrapper">
<textarea rows="5" cols="10" name="Comment"></textarea>
</div>
</td>
</tr>
</table>
</div>
}
#using (Html.BeginForm("SaveWaterReading", "Store", FormMethod.Post, new { id = "myform" }))
{
<div id="waterDialog" style="display: none;" title="Water Readings">
<p>
Please ensure the water readings have been entered!
</p>
<table width="100%">
<tr>
<th>
Serial:
</th>
</tr>
<tr>
<td>
<input type="text" name="WaterSerial" />
</td>
</tr>
<tr>
<th>
Reading:
</th>
</tr>
<tr>
<td>
<input type="text" name="WaterReadingsdfsdf" />
</td>
</tr>
<tr>
<th>
Comment:
</th>
</tr>
<tr>
<td>
<div class="textwrapper">
<textarea rows="5" cols="10" name="WaterComment"></textarea>
</div>
</td>
</tr>
</table>
</div>
}
function showWaterDialog() {
var $dialog = $('#waterDialog').dialog({
autoOpen: false,
modal: true,
width: 400,
height: 400,
buttons: {
Submit: function () {
// $('#frmCreateStore').submit();
var data = $('#frmSaveWaterReading');
$.post("/Store/SaveWaterReading",
$("#frmSaveWaterReading").serialize(),
function () {
$("#waterDialog").dialog("close");
});
//$(this).dialog("close");
},
Cancel: function () {
$(this).dialog("close");
}
}
});
$dialog.dialog('open');
}
In the jquery function for showing the dialog with the form in it, I needed to add that dialog content to the second form on the page as follows:
$("#waterDialog").parent().appendTo($("form").eq(1)[0]);

Resources