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>

3

3 Answers 3

1

How about:

$.ajax({
    url: "<path>/Edit",
    type: "POST",
    data: JSON.stringify(department),
    dataType: "json",
    cache: false,
    success: function (data) {
        [...]
    },
    error: function () {
        [...]
    }
})
Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

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);
                }
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.