-1

I have a razor page (index.cshtml) with two partialViews (parPage1 and ParPage2) Also I have buttons on each of these partialviews.

When I click on button1 on partialView ,should do an ajax post back and to hit the cooreponding action result and load the partialview with a message from ViewBag.Message1 from controller .

SO when I click button1 on my partialView1 ,it should post back and reload with a message saying "Button1 on partialView1 clicked" .

Question

When I click on the button Its is doing an ajax postback to the controller,but its not picking the message from ViewBag to show on the reloaded PartialView page .Why its not picking data from viewbag. When I debug ,I can see ViewBag getting the actual message just before reload on the razor page.But its not showing up on the view .Please help me solve this issue .

After Posting to the controller,I actually need to check if the posted data already exists in db ,and if the data already exists I want to send a message back to the partialView .

Below is my ParPage1.cshtml

@using (Ajax.BeginForm("ParPage1", "Home", new { id = 1 }, new AjaxOptions { UpdateTargetId = "Page1Form" }, new { encType = "multipart/form-data" }))
    {
<div>
<p>This is partial view page1</p>

    <input type="button" title="" value="Button1" id="btnButton1"/>
    <p>@ViewBag.Message1</p>
    </div>

}
<script type="text/javascript">
    $(document).ready(function () {
        $("#btnButton1").click(function (e) {
            var data = { "Data1":"BCXDEFX", "Data2": "BCDEF" };
            $.ajax({
                type: "POST",
                url: "/Home/Button1Clicked",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(data),
                dataType: "json",
                success: saveItemCompleted(data),
                error: saveItemFailed()

            });

        });

        function saveItemCompleted(data) {
        }

        function saveItemFailed(request, status, error) {
        }

    });

</script>

Below is my ParPage2

@using (Ajax.BeginForm("ParPage2", "Home", new { id = 1 }, new AjaxOptions { UpdateTargetId = "Page2Form" }, new { encType = "multipart/form-data" }))
    {
<div>
<p>This is partial view page2</p>
    <input type="button" title="" value="Button2" id="btnButton2"/>

    <p>@ViewBag.Message2</p>
    </div>

  }
<script type="text/javascript">
    $(document).ready(function () {
        $("#btnButton2").click(function (e) {
            var data = { "Data1": "BCXDEFX", "Data2": "BCDEF" };
            $.ajax({
                type: "POST",
                url: "/Home/Button2Clicked",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(data),
                dataType: "json",
                success: saveItemCompleted(data),
                error: saveItemFailed()

            });

        });

        function saveItemCompleted(data) {
        }

        function saveItemFailed(request, status, error) {
        }

    });

</script>

Here is my controller code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication4.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {

            return View();
        }

        public ViewResult ParPage1()
        {
            return View();
        }
        public ViewResult ParPage2()
        {
            return View();
        }
        public ActionResult Button1Clicked()
        {
            ViewBag.Message1 = "Button1Clicked";
            return PartialView("ParPage1");
        }
        public ActionResult Button2Clicked(myData data)
        {
             //Validate for duplicate data
               if(duplicate)
                  {
            ViewBag.Message2 = "This data already exists in table. Please review";
                 }
                 else
                    {SaveRecord();}
            return PartialView("ParPage2");
        }

    }
}
Steve
  • 1,471
  • 7
  • 19
  • 32
  • So what is the question? Are you asking what code to put into the `saveItemCompleted` and `saveItemFailed` handlers? Do you just want to display the message in a popup or something or do you want the partial view to be re-rendered? – asymptoticFault Aug 31 '13 at 00:10
  • When I click on the button Its is doing an ajax postback to the controller,but its not picking the message from ViewBag to show on the reloaded PartialView page .Why its not picking data from viewbag. When I debug ,I can see ViewBag getting the actual message just before reload on the razor page.But its not showing up on the Partial view .Please help me solve this issue . – Steve Aug 31 '13 at 00:58
  • 1
    Usually when we call a controller via ajax and get back a `PartialView` we take the result and put it some place on the form such as `$('#myTargetDiv').html(data);` Have you used the javascript debugger to look at the result (`data`) from your ajax call? – David Tansey Aug 31 '13 at 01:33
  • Here's a highly ranked post that shows a nice example: http://stackoverflow.com/questions/1570127/render-partial-view-using-jquery-in-asp-net-mvc Hope that helps. – David Tansey Aug 31 '13 at 01:38
  • after Posting to the controller,I actually need to check if the posted data already exists in db ,and if the data already exists I want to send a message back to the partialView . – Steve Aug 31 '13 at 23:46

1 Answers1

-1

Maybe try something like this (if error will appera, you should know I technicaly didnt write any application in MVC4 and Jquery :p )

public ActionResult Button1Clicked()
{
        //ViewBag.Message1 = "Button1Clicked";
        //return PartialView("ParPage1");
        return "Button1Clicked"; // return just simple message
}


" <p>@ViewBag.Message1</p> "
change to: <p id="message">not clicked yet</p>



function saveItemCompleted(data) {
       $("#message").text(data);
}

You dont need to download all partial view, you need only a simple string.

apocalypse
  • 5,764
  • 9
  • 47
  • 95