telerik mvc combobox copy and initialize - asp.net-mvc

I am using a Telerik combobox and using jquery to make a clone of it.
the control is being rendered correctly, however the dropdown is not working I believe due to the fact the javascript has not been initialized on the control.
Is there a way to do this or a better way to make a duplicate of the control?
I may have to resort to making an ajax request but would ideally like to keep it all clientside.
Cheers,
Mark

After the clone you need to attach the AJAX call to it as follows.
var c = $('rowTemplate').clone();
c.find('selectorToGetInputControl').tAutoComplete({ ajax: { "selectUrl": "Your Ajax Call" },filter: 1 });

Not sure how you attempt to make a client clone of MVC server control using jQuery, I doubt such thing can operate properly. Either use another instance of the combo or go for pure client-side component.

Related

making unobtrusive validation work when using Select2 ASP.NET MVC

Select boxes converted to Select2, do not automatically integrate with unobtrusive validation mechanism in ASP.NET MVC framework.
For example, on a form which contains a regular select box (marked as required in model definition), submitting the form while no options have been selected in the select box, will cause the border and background of the select box to take a reddish color, and by using #Html.ValidationMessageFor, error messages, if any, can be displayed beside the box. However if the select box is converted to a Select2 component, then none of the mentioned features work any more. Even the validation error message will not show up.
It seems that the reason for even the validation error message not showing, is because Select2 changes the display CSS property of the original select box to none (display:none), and I guess the unobtrusive validation script does not bother generating error messages for invisible fields.
Any ideas / solutions?
This issue isn't really specific to Select2, but rather to the jQuery unobtrusive validator.
You can turn on validation for hidden fields as highlighted in this answer.
$.validator.setDefaults({
ignore: ''
});
As the comments noted, it didn't work inside an anonymous callback function within $(document).ready(). I had to put it at the top level.
I've run into similar issues with the select2 plugin. I don't know exactly which features you're using specifically, but in my experience, when you set an element as a select2 in the document.ready event, the plugin will change some of the element's attributes on the fly (inspect one of the elements after your page has finished loading - oftentimes you'll see the id and class properties are different than what you're seeing when you view source).
It's difficult to offer more without actually seeing the code, but here's a few ideas to get you started:
First off, obviously make sure you have the a link to your select2.css stylesheet in the header.
Then, since you're talking about form submissions, I'd recommend you examine whether or not you're getting a full postback or submitting via AJAX (if you're using jQueryMobile, you're using AJAX unless you override it in the jquerymobile.js file or set a data-ajax="false" in your form attributes). You can just look at the value returned by Request.IsAjaxRequest() for this. Obviously if you're submitting via ajax, you won't hit the document.ready event and the select2 won't initialize properly and you'd need to figure out a way around that. Try refreshing the page after the submit and see if it renders the select2 component.
Then I'd suggest examining the elements and see if they're not behaving like you'd expect because you're actually trying to work with classes that the plugin has reassigned at runtime. You can either just adjust your logic, or you can dig into the select2 code itself and change the behavior - it's actually fairly well-documented what the code is doing, and if you hop on the Google group for select2, Igor is usually pretty quick to follow up with questions.
like this
$('select').on('select2:select', function (evt){
$(this).blur();
});
$('body').on('change', 'select.m-select2', function () {
$(this).blur();
})

Render large portion of page using jQuery & AJAX

I have an ASP.NET MVC application with a page that displays a large table of rows & columns of information.
I have a textbox at the top of the page allowing a user to filter the results in the table. I want the user to be able to start typing a word in the textbox and with each keypress, the results in the table to be updated based on the users filter text.
I've done similar things where I simply return a JsonResult response from my Controller, with a couple of small bits of data, but am not sure of the recommended approach if I want to essentially re-render my whole table (with the new data within it) upon each keypress?
I should also mention that the ViewModel I intend to use when the page is first loaded (prior to any ajax stuff happening) contains an IPagedList, as the table data needs to be paginated and sortable.
What I would do is to work with JQuery ajax API and also with partial views.
Have a look at following article. It displays how you can be able to manipulate your html seamlessly on ASP.NET MVC :
Working With JQuery Ajax API on ASP.NET MVC 3.0 - Power of JSON, JQuery and ASP.NET MVC Partial Views
http://www.tugberkugurlu.com/archive/working-with-jquery-ajax-api-on-asp-net-mvc-3-0-power-of-json-jquery-and-asp-net-mvc-partial-views
Also, following question might help :
How to pass an array through in JQuery Ajax and how to concieve it in server side?
You can use the templates to render the html.
i.e Parametrized html + json = to be rendered html
MicroTemplates
Have a look at http://knockoutjs.com/ it could be a very good fit for you
Take a look at the DataTables plug-in for jQuery. It may match your requirements and provide exactly what you need.

ASP.Net MVC run JavaScript in PartialView when loaded using Ajax.ActionLink

I've got a question regarding ASP.Net MVC.
I'm using an Ajax.ActionLink to load a PartialView.
In this partial view is a javascript function I'd like to get called.
However I can't figure out how to make this happen.
I've tried using AjaxOptions { OnSuccess="functionInPartialView" } when I set the Ajax.ActionLink but for some reason it can't see the Javascript.
EDIT: The PartialView is a mix of JavaScript and Html
I would suggest to use jQuery ($.get/$.ajax). It evaluates the $(function(){}) when you load the partial, so your scripts there fire. And I personally find jQuery easier and cleaner to use.
Add the javascript code in the view that will contain the parcialview, and next use the ajaxOptions { OnSuccess="functionInView" } when you set the Ajax.ActionLink.
If it's only javascript in your partial view, then you should be using a JavaScript Action result, as in this post
Otherwise, the issue is that ajax merely loading content into a div doesn't mean that it executes. In your ajax callback, you need to find the javascript content and eval it, so that your page is aware of the function definition.
I know that's a high level description, but I don't have any samples of doing this. If you post some of the code, maybe someone can suggest a cleaner way of doing this so that you have better access to the script.
You can try Multipartials , you can use them to update multiple views and even run scripts from partials, im not sure about the exact specifics as its been a while, but you can have a look at it and see if it can accomplish what you are looking for

Creating an Ajax.ActionLink using javascript

I have a Todo List (an ASP.MVC UserControl) that for each row has the name of the task (that can be clicked to toggle between done/outstanding) and a delete button for removing the task - Both are created using Ajax.ActionLink to query the server.
When creating a new row I need the task text and delete button to be ActionLinks, how would I do this from the client side? Is there an Ajax.ActionLink equivalent in the Microsoft Ajax JS libraries?
Take a look at the code that is being generated using View Source. All you need to do is add the same markup and add the correct click handler via javascript. You may find it actually easier to do this with jQuery for all elements and add the jQuery code in your UserControl. At least that way you control the code and if Microsoft decides to change the way their ActionLink works, you won't get caught short.

FCKEditor in asp.net-mvc

How to add FCKEditor in MVC application?
How to show database value comes in model, in FCKEditor?
That CodeProject website isn't ideal. It asks you to do alot of unneccessary code. All you really need to do is include the correct javascript file:
Then, in the page, render the FCKEditor, given any number of different ways. I prefer to replace a text area:
window.onload = function()
{
var oFCKeditor = new FCKeditor( 'content' ) ;
oFCKeditor.ReplaceTextarea() ;
}
At that point, the editor should load just fine. You will probably need to edit the fckeditor configuration files to get the standardized behavior you want. At this point, however, everything should just work. Your FCKEditor instance will behave just like another form field, and you can treat it as such when you get values from it on the server side.
It's very easy to create the server side api's for it to use as well. I created an fckeditor control, and you just need to implement GetFolders, GetFoldersAndFiles, and GetFiles. Those only take a few lines and give you nearly all the functionality you need.
I think it's easier to integrate / customize fckeditor using MVC than it is on Classic ASP.NET.

Resources