cannot applybindings multiple times knockout in MVC partial view - asp.net-mvc

Hi have a parent page in which i have used knockout js to bind model with html element.
Now i make a ajax call to receive a partialviewresult which i place it in a div conbtainer.
All works fine if use the inbuilt mvc model binding.
But when i go for knockout in my partial view as well. I get the errorcannot applybindings multiple times knockout in MVC partial view.
I have even tried using
ko.applybindings(new vm(),document.getelementbyId("div1"))
ko.applybindings(new vm1(),document.getelementbyId("div2"))
But still get the same error. Is it not possible to get the partial view result from the action method and use knockout in partial view ? I do not want hide the div in my parent page and get a JsonResult and bind it to my div element.

If you have the following (general layout):
<div id="parent">
content
<div id="partialTarget"></div>
</div>
and you've already applied your bindings to #parent, you have to clean #partialTarget before applying the viewmodel again. #partialTarget has already been bound from the first pass, so to apply the bindings to the loaded contents, you need to do something like this:
var reapplyBindings = function(element){
var vm = ko.dataFor(element);
if( vm ) {
ko.cleanNode(element);
ko.applyBindings(vm, element);
}
};
element.load(‘path/to/fragment.html’, function() {
//the [0] selector is needed to be sure we have an actual dom element, not the jQuery wrapper
reapplyBindings(element[0]);
//do whatever you’re already doing
});

Related

How to communicate between mvc partial view and angular component

How can i call a method inside the angular component when i change the drop down in the partial view.
I have a Partial view with a drop down control and an angular component in my mvc page. When i change my drop down in the partial view(created using razor engine), i need to trigger a method inside the angular component.
I found a workaround.
Add a input on the angular component html
<input type="hidden" id="selDate"/>
attach an onChange event in the corresponding .ts file
$(function () {
$('#selDate').on('change', function () {
var dt = $('#selDate').val();
self.getIRData(dt);
});
});
set the value of the above control from the partial view(dropdown change event) and simulate an onChange event by adding Dispatchevent()
please let me know if you have any better workarounds

Cannot apply bindings multiple times - when binding a ViewModel in Layout and Content page

I'm trying to convert my MVC app's _Layout page to use a knockout viewmodel instead of Razor syntax. So far, all of my content pages have syntax like the following to render javascript ViewModels (for Index view):
<script type="text/javascript">
$(document).ready(ko.applyBindings(new IndexVm(
#Html.Raw(JsonConvert.SerializeObject(Model, new JsonSerializerSettings() { ContractResolver = new CamelCasePropertyNamesContractResolver() })))));
</script>
This has been working great so far. Now, in my Layout, I tried to use the same approach with the following:
<script type="text/javascript">
$(document).ready(ko.applyBindings(new LayoutVm(
#Html.Raw(JsonConvert.SerializeObject(ViewBag, new JsonSerializerSettings() { ContractResolver = new CamelCasePropertyNamesContractResolver() })))));
</script>
If the content page doesn't have an inner viewmodel declaration, this works. But when I load up the Index page (with the first snippet), I get the following:
Uncaught Error: You cannot apply bindings multiple times to the same element.
I'm a bit stumped as to why this isn't working. Any help would be much appreciated!
You must understand that ASP.NET will accomplish certain tasks on the server before sending an HTML document to the browser. It will interpret the Razor statements as well as assemble the partial views, views and layout together into a single HTML document on the server.
Knockout is framework that works on the client (browser). If you apply bindings in your _Layout.cshtml and your Index.cshtml, you be applying bindings twice in the assembled HTML document. In Knockout, you can't apply bindings multiple times on the same HTML elements.
What you will need to do is add an id attribute with a meaningful value to certain HTML elements. Then, you will need to add a second parameter to your different ko.applyBindings which will be the element id.
Now you can always control the descending bindings yourself if you have a case where there are nested elements. You can specify a statement that will stop the binding of a parent element from going down the child elements. Find out more about this on http://www.knockmeout.net/2012/05/quick-tip-skip-binding.html.
Well you can do this by simply providing placeholders in html:
<div id="index">
<!-- index page goes here -->
</div>
and
<div id="layout">
<!-- layout page goes here -->
</div>
Then you can apply view model as follows:
<script type="text/javascript">
$(document).ready(ko.applyBindings(new IndexVm(
#Html.Raw(JsonConvert.SerializeObject(Model, new JsonSerializerSettings() { ContractResolver = new CamelCasePropertyNamesContractResolver() }))), document.getElementById('index')));
</script>
and
<script type="text/javascript">
$(document).ready(ko.applyBindings(new LayoutVm(
#Html.Raw(JsonConvert.SerializeObject(ViewBag, new JsonSerializerSettings() { ContractResolver = new CamelCasePropertyNamesContractResolver() }))), document.getElementById('layout')));
</script>
ko.applyBindings can take two parameters, the first being your viewmodel, and the second being an optional root node for the binding context to be set. if no value is supplied, the root node default is window.document.body. if you call applyBindings twice without specifying different root nodes, then it will give you the error you are receiving.
keep in mind, that you do not want to overlap nodes that are being bound. if you need to call applyBindings twice for two separate viewmodels, you must specify different elements to bind to:
ko.applyBindings(new MenuVM(), document.getElementById('menu'));
ko.applyBindings(new ContentVM(), document.getElementById('sub-content'));
edit
based on rwisch45's comment, an option is to have a single viewmodel be bound to the entire page, and set child viewmodels inside the main viewmodel.
http://jsfiddle.net/TNR89/

Loading JQuery scripts to a partial view rendered by ajax call

Following is my view which contains a div in which partial views are rendered based on actions performed in the view.
//Boiler Plate HTML
<div id="PartialViewHolder">
#Html.Partial(Model.PartialView, Model)
</div>
//Boiler Plate HTML
The partial views are rendered via an ajax call which is as follows
//The url is supplied based on some actions in the main view
function AjaxCall(url){
$.ajax({
url: url,
cache: false,
success: function (html) {
$("#PartialViewHolder").empty();
$("#PartialViewHolder").html(html);
},
error: function (result) {
alert("Error: " + result.status + ": " + result.statusText);
}
});
}
The main page also loads a few other scripts which are common to the partial views. These scripts work when the page is first rendered, i.e when the default partial view is rendered. However these scripts stop working for partial views which are loaded by the ajax call. I believe that these scripts need to be reloaded when the DOM elements change. I am looking for a clean way to reload those scripts when the partial view div is reloaded.
You need to do a bit of reading about Event binding. http://api.jquery.com/on/
Tricky to understand at first but worth it once you do.
Assuming you've got some code like this.
<div class="container">
<div class="partial">
Click here to do stuff
</div>
</div>
JS Code like the example below will only work for objects present when you do the binding (usually document.ready()) It seems like this is the situation you are describing.
$(".magic-link").click(function(){
alert("Magic link clicked");
return false;
})
If you want the event to fire on objects that haven't yet been loaded onto the page then you need to bind an event handler to the container div i.e. the bit which doesn't change.
Like this
$(".container").on("click", ".magic-link", function(){
alert("Magic link clicked");
return false;
});
The event bubbles up to the event handler and fires the event. This has another advantage that one event handler and potentially handle events from hundreds of objects.
As a guideline try to bind the listener to the nearest parent object that will not change. In the case of the example this is the Container div. This keeps the bubbling to a minimum.

ASP.NET MVC 3 - tell partialview to regrab the viewdata values?

Let's say I have a partial view X.ascx like this:
<div id = "updateTargetIdForAjaxForm">
... javascript code which only has function definitions.
... ajax form with buttons inside
<div id = "stuff"></div>
<script type = "text/javascript">
do things to "stuff" div as soon as X.ascx is loaded.
</script>
</div>
Now I want to update things on X.ascx, based on Ajax response. Earlier, I was returning PartialView but inline javascript that does things to stuff div wouldn't like that, meaning the inline javascript just won't get executed when X.ascx is reloaded.
So is there a way I can not return PartialView in my controller, but just update ViewData values and tell the PartialView to re-grab the updated ViewData values? And also call one or two javascript functions based on the response from server maybe?
I see two possible ways to go:
Fixing your javascript code to run with dynamically loaded partial view:
http://api.jquery.com/live/
http://api.jquery.com/delegate/
These two should help you out.
The other way is to create an action on your controller that would return JSON result and from jQuery you should be able to handle JSON object and update your form. Your Action could look like :
public JsonResult GetFormValues()
{
var jsonFormValues = new
{
key1 = "abc",
key2 = "123",
key3 = "abcdef"
};
return Json(jsonFormValues, JsonRequestBehavior.AllowGet);
}
and check out http://api.jquery.com/jQuery.getJSON/ on how to handle the result

ASP MVC + AJAX, trying to Update a div asynchronously

I'm new to Asp MVC, and I'm trying to accomplish a little async update (I'm using MVC3 RC2, with Razor, but I can manage with ASPX solutions too).
I have a Master page, which renders a shopping cart box on every page by calling Html.RenderAction("Cart","Shop"). The Cart action of the ShopController calls the database, etc, and outputs the results. This works.
First problem: if I put an ActionLink in this partial view (like Html.ActionLink("Remove")), then even if I call PartialView() from the Remove action, it renders only the shopping cart, and nothing else (in essence, my page disappears).
Second problem: There is a div called "cartContent" inside this partial view. I want to be able to put a link ANYWHERE (not just on the Master page or in the partial view), which when pressed calls a controller Action, and then updates ONLY the cartContent div based on the results. I've tried Ajax.ActionLink but it does the same as Html.ActionLink, even though I imported the Microsoft.MvcAjax js libs.
Also, if I solve the first problem, I want that to be async as well.
What solution do I use? I've tried setting UpdateTargetID to "cartContent", I've tried wrapping the cartContent into an Ajax.BeginForm, nothing. Do I HAVE to use jQuery (which I know nothing of)? Do I serialize some response to JSON, and update the div manually in Javascript? (I'm not really good at JS, I'm coming from the C# side of things)
You put a link wherever you want:
#Html.ActionLink("remove item", "remove", "somecontroller",
new { id = Model.Id }, new { #class = "remove" })
And then in a separate javascript file:
$(function() {
$('.remove').click(function() {
// when the link is clicked
// perform an ajax request:
$.ajax({
url: this.href,
type: 'delete',
success: function(result) {
// when the AJAX call succeed do something with the result
// for example if the controller action returned a partial
// then you could show this partial in some div
$('#someDivId').html(result);
}
});
// don't forget to cancel the default action by returning false
return false;
});
});
Remark: if the div you are updating contains also the link then you might need to use the .live() function or the click event handler will not work the second time because the DOM will be modified:
$('.remove').live('click', function() {
...
});

Resources