1

Can somebody give me a hint how to pass a list from the controller to Model list in view page after call the Action Result whit ajax in page. (Meaning update current list model with ajax call back result)?

This is my default load view page code:

@model List<ChargeSystem.Models.Message>
@foreach (var item in Model)
    {
        <div class="container1">
                <p>@item.Msg</p>
                <span class="time-right">@item.MsgDate</span>
            </div>
    }

</div>
<div class="divContinMsg">
    <input type="text" id="txtMsg" name="txtMsg" />
</div>
<script type="text/javascript">
    $(document).ready(function () {
        $("#txtMsg").keyup(function (e) {
            if (e.keyCode == 13) {
                $.ajax(
                    {
                        url: '/User/ajaxContactAdmin?msg=' + $("#txtMsg").val(),
                        type: 'Post',
                        data: "",
                        contentType: false,
                        success: function (result) {
                        //What can i do????
                        },
                        error: function () {
                            alert("error");
                        }
                    })
            };
        });
    });
</script>

This is the Ajax call action result:

 public ActionResult ajaxContactAdmin(string msg)
        {
            var result = new { model = messageRepository.Select().ToList()};
            return Json(result, JsonRequestBehavior.AllowGet);
        }

So, How can i refresh the model after ajax call back?

1
  • what does result has in it ? Add that as well . Commented Aug 11, 2020 at 3:51

2 Answers 2

1

So what you would do is append the result to the existing result set.

Firstly I would add a container for easier reference, secondly you would add the item to the container:

@model List<ChargeSystem.Models.Message>
<div id="listContainer">
  @foreach (var item in Model)
    {
        <div class="container1">
                <p>@item.Msg</p>
                <span class="time-right">@item.MsgDate</span>
            </div>
    }
  </div>
</div>
<div class="divContinMsg">
    <input type="text" id="txtMsg" name="txtMsg" />
</div>
<script type="text/javascript">
    $(document).ready(function () {
        $("#txtMsg").keyup(function (e) {
            if (e.keyCode == 13) {
                $.ajax(
                    {
                        url: '/User/ajaxContactAdmin?msg=' + $("#txtMsg").val(),
                        type: 'Post',
                        data: "",
                        contentType: false,
                        success: function (result) {
                         $('#listContainer').append('<div class="container1">'+
                             '<p>' + result.Msg + '</p>'+
                             '<span class="time-right">' + result.MsgDate +'</span>'+
                             '</div>');
                        },
                        error: function () {
                            alert("error");
                        }
                    })
            };
        });
    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

It looks like you want to enter information in your text box and save and update it in the view. I think you can do this.

Here is an example:

Your Controller:

public IActionResult GetUser ()
        {
            var messages = context.Messages.ToList();
            return View(messages);
        } 
        [HttpPost]
        public IActionResult ajaxContactAdmin(string msg)
        {
            var message = new Message
            {
                Msg = msg,
                MsgDate = DateTime.Now
            };
            context.Add(message);
            context.SaveChanges();
            return Json(message);

        }

Js in your View:

@section scripts{ 
    <script>
        $(document).ready(function () {
            $("#txtMsg").keyup(function (e) {
                if (e.keyCode == 13) {
                    var msg = document.getElementById("txtMsg").value
                    $.ajax(
                        {
                            url: '/Home/ajaxContactAdmin?msg=' + $("#txtMsg").val(),
                            type: 'Post',
                            data: { "msg": msg},
                            contentType: false,
                            success: function (message)
                            {
                                console.log(message);
                                window.location.reload();
                            },
                            error: function () {
                                alert("error");
                            }
                        })
                };
            });
        });
    </script>
}

Result display: enter image description here

2 Comments

You should no be doing reload on success
Thank you dear, but the location.reload(); is not real time change. its look like server side responded.

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.