0

I want to pass the model from controller to Success method in View and access the properties from the model with json. How do I write and how to access the properties in Success method?

  public async Task<IActionResult> Edit( Department department)
    {

        if (ModelState.IsValid)
        {
            _genericRepository.Update(department);
            await _genericRepository.SaveChangesAsync();
            var model = _genericRepository.GetByIdAsync(department.Department_Id);
            return Json(new { Message = model });
        }
        return Json(department);
    }

   <script>
        function Success(data)
        {
            alert(data.Messge);

        }
        function Failure() {          
        }
</script>
OrionMD
  • 389
  • 1
  • 7
  • 13
mahdi
  • 93
  • 1
  • 11
  • Use AJAX call method and return the data in success.! – Mohan Srinivas Jul 03 '18 at 07:56
  • my forms work with ajax , how to send my model from controller to view with json? @MohanSrinivas – mahdi Jul 03 '18 at 07:59
  • Refer this https://www.c-sharpcorner.com/UploadFile/cb1429/view-model-and-sending-complex-json-objects-to-Asp-Net-mvc-v/ and this https://stackoverflow.com/questions/15196528/how-to-return-json-object-from-mvc-controller-to-view – Hitesh Anshani Jul 03 '18 at 08:02

3 Answers3

1

How about:

$.ajax({
    url: "<path>/Edit",
    type: "POST",
    data: JSON.stringify(department),
    dataType: "json",
    cache: false,
    success: function (data) {
        [...]
    },
    error: function () {
        [...]
    }
})
Svetoslav Petrov
  • 1,036
  • 1
  • 11
  • 33
0

You can access data list from json file when using Json.Parse();

<script>
        function Success(data)
        {
             var getDataList = Json.parse(data);
            alert(getDataList);

        }
         function Failure() {       

          }
</script>
Kiran Joshi
  • 1,758
  • 2
  • 11
  • 34
Jamal Kaksouri
  • 1,684
  • 15
  • 22
0
public ActionResult Import()
    {
       string response = JsonConvert.SerializeObject(responseModel);

       return this.Content(response);

    }

class ResponseModel 
      {
        public string bodyone { get; set; }
        public string bodytwo { get; set; }

        public string HeadersFile1 { get; set; }
        public string HeadersFile2 { get; set; }

        public string FileName1 { get; set; }
        public string FileName2 { get; set; }

   }

then parse the response using JSON parser. Now you can read property values from ajax success like this

   success: function (response) {

                if (response.length == 0)
                    alert('Some error occured while uploading');
                else {
                    var obj = JSON.parse(response); 

                    $('#divPrint').html(obj.bodyone);
                    $('#divPrint2').html(obj.bodytwo);

                    $('#fileName1').html(obj.FileName1);
                    $('#fileName2').html(obj.FileName2);

                    $('#headersFile1').html(obj.HeadersFile1);
                    $('#headersFile2').html(obj.HeadersFile2);
                }
            }