View层:
$('#btnNews').click(function() { if (!confirm('确定要提交吗?')) { return; } var frontViewData = []; var title = $("*[name='title']").val(); var digest = $("*[name='digest']").val(); var showCoverPic = $("*[name='show_cover_pic']").val(); var content = CKEDITOR.instances.content.getData(); frontViewData.push({ Title: title,Digest: digest, ShowCoverPic: showCoverPic, Content: content }); $.ajax({ type:'POST', url: '/Media/AddArticle', data: JSON.stringify(frontViewData), contentType: 'application/json;charset=utf-8', success: function(json) { alert(json.Message); } }); });
Controller层:
public ActionResult AddArticle(List<ArticleViewModel> frontViewData) { var file = TempData["savePath"].ToString(); var accessToken = AccessTokenContainer.GetAccessToken(AppId); try { //上传临时素材,测试账号用,永久素材上传一会后就会返回错误代码45009 //var mediaId = MediaApi.UploadTemporaryMedia(accessToken,UploadMediaFileType.thumb, file).thumb_media_id; //上传永久素材 var mediaId = MediaApi.UploadForeverMedia(accessToken, file); var news = new NewsModel() { author = "呵呵哒", content = frontViewData[0].Content, content_source_url = "", digest = frontViewData[0].Digest, show_cover_pic = frontViewData[0].ShowCoverPic, thumb_media_id = mediaId.media_id, title = frontViewData[0].Title, }; //上传临时素材,测试账号用,永久素材上传一会后就会返回错误代码45009 //var result1 = MediaApi.UploadTemporaryNews(accessToken, 10000, news); //上传永久素材 var result1 = MediaApi.UploadNews(accessToken, 10000, news); var json = new { Success = result1.errmsg == "ok", Message = "新增成功" }; return Json(json); } catch(Exception ex) { var json = new { Message = ex.Message }; return Json(json); } }
ViewModel层:
public class ArticleViewModel { public string Title { get; set; } public string ThumbUrl { get; set; } public string Digest { get; set; } public string ShowCoverPic { get; set; } public string Content { get; set; } }
还可以利用Request.Form[""]进行传值不过必须是inout中的name属性