Sorry for long question. I just want to make clear what I'm asking.
I've a JSON result in my Controller as:-
public JsonResult Top7Video()
{
db.Configuration.ProxyCreationEnabled = false;
var result = (from v in db.TblVideos
orderby v.FileName
select new { title= v.FileName, artist=v.Artist,Image=v.Image,ID=v.ID}).Take(7).ToList();
return Json(result,JsonRequestBehavior.AllowGet);
}
And in my Razor View I want it as a JQuery array. I tried following approach:-
<script type="text/jscript">
var myarray = '{"title": "3. Ellie-Goulding","artist": "","mp4": "~/video/Ellie-Goulding.mp4","ogv": "~/video/Ellie-Goulding.ogv","webmv": "~/video/Ellie-Goulding.webm","poster": "~/video/VideoImg/play1.png"}';
//var myarray = '{"Err0":"Only letters and white space allowed in Name"}';
//alert(myarray);
$(function () {
$.ajax({
type: "GET",
url: "/Home/Top7Video",
data:"{}",
datatype: "Json",
success: function (data) {
$.each(arr, function (index, value) {
myarray.push([',{ "title": "' + value.FileName + '", "artist": "' + value.Artists + '", "mp4": "~/video/' + value.FileName + '", "ogv": "~/video/' + value.FileName + '", "webmv": "~/video/' + value.FileName + '", "poster": "~/video/VideoImg/' + value.Image + '" }']);
});
}
});
});
var arr = $.parseJSON(myarray); //convert to javascript array
alert(arr);
$(function () {
$.ajax({
type: "GET",
url: "/Home/Top7Video",
datatype: "Json",
success: function (data) {
$.each(data, function (index, value) {
$('#dropdownVideo').append('<li><div><a href="javascript:;" style="display: none;">×</a><a href="javascript:;" class="jp-playlist-item jp-playlist-current" tabindex="0">' + value.FileName + '<span class="jp-artist"> Song by ' + value.Artist + '</span></a></div></li>');
});
}
});
});
$(document).ready(function () {
new jPlayerPlaylist({
jPlayer: "#jquery_jplayer_1",
cssSelectorAncestor: "#jp_container_1"
},
arr,
{
swfPath: "../../dist/jplayer",
supplied: "webmv,ogv,mp4",
useStateClassSkin: true,
autoBlur: false,
smoothPlayBar: true,
keyEnabled: true
});
});
</script>
Following is my HTML:-
<div class="video-main">
<div class="video-record-list">
<div id="jp_container_1" class="jp-video jp-video-270p" role="application" aria-label="media player">
<div class="jp-type-playlist">
<div id="jquery_jplayer_1" class="jp-jplayer" style="width: 480px; height: 270px;">
<img id="jp_poster_0" src="~/video/VideoImg/play2.png" style="width: 480px; height: 270px; display: inline;">
<video id="jp_video_0" preload="metadata" src="~/video/Ellie-Goulding.mp4" title="1. Ellie-Goulding" style="width: 0px; height: 0px;"></video>
</div>
<div class="jp-gui">
<div class="jp-video-play" style="display: block;">
<button class="jp-video-play-icon" role="button" tabindex="0">play</button>
</div>
<div class="jp-interface">
<div class="jp-progress">
<div class="jp-seek-bar" style="width: 100%;">
<div class="jp-play-bar" style="width: 0%;"></div>
</div>
</div>
<div class="jp-current-time" role="timer" aria-label="time">00:00</div>
<div class="jp-duration" role="timer" aria-label="duration">00:30</div>
<div class="jp-controls-holder">
<div class="jp-controls">
<button class="jp-previous" role="button" tabindex="0">previous</button>
<button class="jp-play" role="button" tabindex="0">play</button>
<button class="jp-next" role="button" tabindex="0">next</button>
</div>
<div class="jp-volume-controls">
<button class="jp-mute" role="button" tabindex="0">mute</button>
<button class="jp-volume-max" role="button" tabindex="0">max volume</button>
<div class="jp-volume-bar">
<div class="jp-volume-bar-value" style="width: 100%;"></div>
</div>
</div>
<div class="jp-toggles">
<button class="jp-full-screen" role="button" tabindex="0">full screen</button>
</div>
</div>
<div class="jp-details" style="display: none;">
<div class="jp-title" aria-label="title">1. Ellie-Goulding</div>
</div>
</div>
</div>
<div class="jp-playlist">
<ul id="dropdownVideo" style="display: block;">
<li class="jp-playlist-current">
<div>
<a href="javascript:;" class="jp-playlist-item-remove" style="display: none;">×</a>
<a href="javascript:;" class="jp-playlist-item jp-playlist-current" tabindex="0">
1. Ellie-Goulding
<span class="jp-artist">by Pixar</span>
</a>
</div>
</li>
</ul>
</div>
<div class="jp-no-solution" style="display: none;">
<span>Update Required</span>
To play the media you will need to either update your browser to a recent version or update your <a href="http://get.adobe.com/flashplayer/" target="_blank">Flash plugin</a>.
</div>
</div>
</div>
</div>
</div>
I'm Not able to convert the JSON into Jquery aray. I get following error:-
Uncaught TypeError: myarray.push is not a function
I'm New into JSON and searched a lot for solution here How to response an array in json to jquery? and How to convert Json object to Jquery Array?. But I'm not able to solve this. PLease HELP!!
myarrayis a string, not an array. Maybe you meant to use brackets[]instead of quotes''?datais already an array. And if you want those property names (title,artistetc) then modify your controller to send an collection of anonymous objects with those property names -select new { title = v.FileName, .... }