MVC Widget with dynamic content
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
>
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>
Hello,
Here is a video (screencast.com/.../zTCiWxpjVXBb) of the widget on my end. Hope you find it useful.
Regards,
Nikola Zagorchev
Telerik