zoukankan      html  css  js  c++  java
  • 微信上传文章素材—ASP.NET MVC从View层传数据到Controller层

    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属性

  • 相关阅读:
    从体制内的国家干部转变为自由职业者-2017年总结
    为什么选择Django?
    Django contrib Comments 评论模块详解
    Django 2.0 新特性 抢先看!
    Python 为何能坐稳 AI 时代头牌语言
    为什么说Python 是大数据全栈式开发语言
    继承中的构造方法
    super关键字
    方法的重写
    类的继承与访问控制
  • 原文地址:https://www.cnblogs.com/xiaomen/p/6089354.html
Copyright © 2011-2022 走看看