2

Before using Ajax, I used to return data with ViewBag or TempData properly. However, when using Ajax, none of the prior methods do not work even if trying to force with Javascript. Could you please clarify me where I made mistake?

Controller:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Exclude = null)] MyModel model)
{
    //... removed for brevity  
    TempData["message"] = "My tempdata message";
    ViewBag.message= "My viewbag message";
}

View:

/* Method I: I tried to show data as I used to before using Ajax. But, now it 
is not displayed as below (Normally I use one of them, not both of course) */
@if (TempData["message"] != null || ViewBag.message != null)
{
    //The code hits here but the message is not displayed
    <div class="text-danger">@TempData["message"]</div>
    <div class="text-danger">@ViewBag.message</div>
}
else
{
    <div class="noMessage"></div>
}


Method II:
<div class="text-danger" id="divSuccessMessage" style="display:none;">@ViewBag.message</div>    


<script type="text/javascript">
$(function () {

    $('form').submit(function (event) {
        event.preventDefault();            
        var formdata = new FormData($('#frmCreate').get(0));
            $.ajax({
                type: "POST",
                url: '@Url.Action("Create", "Issue")',
                data: formdata,
                processData: false,
                contentType: false,
                error: function (response) {
                    if ('@ViewBag.message' != "") {
                        $('#divSuccessMessage').show();
                        alert('test'): //The code hits here, but the message 
                        //is not displayed in divSuccessMessage with ViewBag or Tempdata. 
                        //On the other hand, if I use just a text in divSuccessMessage, it is displayed.
                    }
                }
            });
        }

});
Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
Jack
  • 1
  • 21
  • 118
  • 236
  • 1
    Your not returning a view back to the client so `response` is `null` Why not just return json containing the message? –  Jun 21 '15 at 09:42
  • @StephenMuecke First of all many thanks for your helps for lots of my problem for the last days :) On the other hand, as I said to your in the prior messages, I have no experience and not know to which way to go. So, I would be appreciated if you provide a sample as you said or as most suitable way for my situation. – Jack Jun 21 '15 at 09:46
  • @StephenMuecke Please also attention that, I use HttpPost and not sure if I can use Json (as I pass data to the database). – Jack Jun 21 '15 at 09:49
  • It really depends what your trying to do. If all you want is to display a message after you post, then in the controller, use `return Json("your message");` and in the ajax `success: function(response) { $('#divSuccessMessage').text(response);` –  Jun 21 '15 at 09:50
  • 1
    You pass form data to the controller to save it, but you can return Json (or html or just simple text) –  Jun 21 '15 at 09:52
  • I want to pass data from Controller to the View and I want to type the message in the Controller rather than View. Because the message is typed according to the different situation in the Controller i.e. validation, exception... Any idea? – Jack Jun 21 '15 at 09:55
  • You are passing (@ViewBag.message) from the controller and using (@ViewBag.successMessage) in the view. They should be the same spelling. – Ala Jun 21 '15 at 09:56
  • @StephenMuecke: Thanks. It is working. In that case I think there is no need to use `return PartialView("Create", model);` as I just return message and Ajax request returns automatically to the View that calling it. Is that true? message – Jack Jun 21 '15 at 10:04
  • @Ala: Thanks, but I mistyped at here. Normally of course I use the same name and I updated the code. – Jack Jun 21 '15 at 10:05
  • 1
    If you only want to display a message in view, then yes. Alternatively if you want to update the DOM with a more significant change, then you would return a partial view - but the partial needs to access the `ViewBag` property (and render it to the partial). You cant access a `ViewBag` property it in the ajax callback –  Jun 21 '15 at 10:08
  • @Many thanks again for your helps. With the help of you, I have solved 3 big problem for creating a new record on popup window and I can proceed :) Voted+++ – Jack Jun 21 '15 at 10:11

0 Answers0