+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / MVC Widget with dynamic content

MVC Widget with dynamic content

3 posts, 0 answered
  1. Damein
    Damein avatar
    9 posts
    Registered:
    10 Apr 2012
    31 Jul 2014
    Link to this post

    Hi Everyone.

    I've been trying to make a MVC based widget that has some dynamic content i.e. a typeahead suggestion for input box.

    I've create two actions on my controller. Index which returns the view (the actual widget) and action Search that returns a list of names as Json result.

    My problem is that I can't get Search action to work.

    If i call it using jquery $.ajax method I get a 404 not found. The link i'm using is /<url of page widget is on>/Search

    is this not correct? what should this be?

    I've included some very basic code below of my setup

    Controller

    1.      public ActionResult Index(){
    2.return View();
    3.        }
    4. 
    5.public ActionResult Search(string name){
    6.var names = new List<string> { "bob","john" };
    7.return Json(names.ToArray(), JsonRequestBehavior.AllowGet)
    8.}

    View

    01.<ul id="names">
    02.</ul>
    03. 
    04.<script>
    05.$.ajax({
    06.url: '/mytestpage/Search',
    07.data: { name: "john" }
    08.}).done(function(data){
    09.var html = [];
    10.for(var i in data){  
    11.var li = $('<li/>',{
    12.text: data[i];
    13.});
    14.html.push(li);
    15.}
    16.$('#names').html(html)
    17.});
    18.</script>

     

     

  2. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    08 Dec 2016
    05 Aug 2014
    Link to this post
    Hi Damein,

    What version of Sitefinity are you using? I tested on Sitefinity 7.1 on my end and the Action is successfully called and executed. Please, change your code in the JavaScript to use the for loop instead of for-in, since for-in iterates through the object properties and you will probably get a 'false' record after the 'bob' and 'john':
    <ul id="names">
    </ul>
      
    <script>
        $.ajax({
            url: '/mvc/Search',
            data: {
                name: "john"
            }
    }).done(function(data){
        var html = [];
        for (var i = 0; i < data.length; i++) {
            var li = $('<li/>',{ text: data[i] });
            html.push(li);
        }
        $('#names').html(html);
    });
    </script>

    You could also try to call directly the Url (/page/Search) in the browser and see if it is resolved by the Mvc widget controller.

    Regards,
    Nikola Zagorchev
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  3. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    08 Dec 2016
    05 Aug 2014
    Link to this post
    Hello,

    Here is a video (http://screencast.com/t/zTCiWxpjVXBb) of the widget on my end. Hope you find it useful.

    Regards,
    Nikola Zagorchev
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
3 posts, 0 answered