Home > front end >  I can't pull data from ajax
I can't pull data from ajax

Time:01-06

 public enum SocialEnum
    {
        [Display(Name = "Genel")]
        General,
        [Display(Name ="Teşekkür Et")]
        Thanks
    }
    public class SocialMedia : Entity<string>
    {
        public string Message { get; set; }

        public string Departmen { get; set; }
        public SocialEnum MessageType { get; set; }

        public string FullName { get; set; }
}

This is my model

<div  id="SubjectButtons">
                                <button  id="general"><i ></i>Genel</button>
                                <button  id="thanks"><i ></i>Teşekkür Et</button>
                            </div>

this is my cshtml

 $(function () {
            $('#Share').on('click', function () {
             
                var file = $("#imgupload").get(0).files;
                var droplist = $(".ui.fluid.search.dropdown").val();
                var message = $(".ui.form").val();
                var sbjtbtn = $("#general").val();
                var sbjtbtn = $("#thanks").val();
                
                var data = new FormData;
                data.append("Photo", file[0]);
                data.append("Message", message);
                data.append("FullNameList", droplist);
                data.append("MessageType", sbjtbtn);
               

                $.ajax({
                    url: '@Url.Action("Index")',
                    type: "POST",
                    data: data,                  
                    contentType: false,
                    processData: false,
                    success: function (data) {
                        $(".post-area").html(Counter);
                            $(".post-area").html(data);
                            $("#message").html(data.message);
                            $(".img-responsive").append('<img src="/Image/'   data   '"class=img-responsive thumbnail"/>');
                        
                        
                        if (sbjtbtn == $("#thanks")) {
                           $("#person").html(data.droplist);
                           $(".post-area").html(data);
                            $("#message").html(data.message);
                            $(".img-responsive").append('<img src="/Image/'   data   '"class=img-responsive thumbnail"/>');
                        }

                    },
                    error: function (data) {

                    }

                });
            });
        });

this is my js

public ActionResult Index(SocialMedia data)
        {          
                var model=GetSocialMedia();
                MediaList mediaList = new MediaList();
            if (mediaList.MessageType == data.MessageType)
            {
                mediaList.FullName = model.FullName;
                mediaList.Departmen = model.Departmen;
                mediaList.Message = data.Message;
                var file = data.Photo;
                if (file != null)
                {
                    string Location = Server.MapPath("/Image/"   file.FileName);
                    file.SaveAs(Location);
                    mediaList.Photo = "../Image/"   file.FileName;
                }
                mediaList.FullNameList = data.FullNameList;
            }             
            return PartialView("~/Views/SocialMedia/MediaList.cshtml", mediaList);
         
        }

This is my controller

When you press the general button, some data should come. but if you press the thanks button, it should pull more data. I have defined it separately in ajax. I gave the variable name the same. message type comes in general.The message type is never be thanks.Where is my mistake? My index page, model and controller are longer, but I think these are the parts I need to show.Sorry for my English:)

CodePudding user response:

you should know already what is the difference between view and model. In any case in index view place this div

.....

<div id="mediaList">
<partial name="~/Views/SocialMedia/MediaList.cshtml"  />
</div>
.....

<div  id="SubjectButtons">
<button  id="general"><i ></i>Genel</button>
  <button  id="thanks"><i ></i>Teşekkür Et</button>
</div>

and ajax

                $.ajax({
                    url: '@Url.Action("Index")',
                    .....

                    success: function (data) {
                     $("#mediaList").html(data);
                     },
                     error: function (xhr) {

                    }
                });

CodePudding user response:

have you tried using POSTMAN tool? https://www.postman.com/ . This tool is used for testing webservice API. Try this one first if you really get some data from your API URL.

  •  Tags:  
  • Related