Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC4 Jquery Ajax Post back partial view and get ViewBag data from controller
    primarykey
    data
    text
    <p>I have a razor page (index.cshtml) with two partialViews (parPage1 and ParPage2) Also I have buttons on each of these partialviews.</p> <p>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 .</p> <p>SO when I click button1 on my partialView1 ,it should post back and reload with a message saying "Button1 on partialView1 clicked" .</p> <p><strong>Question</strong> </p> <p><em>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 .</em></p> <p><strong>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 .</strong></p> <p>Below is my ParPage1.cshtml</p> <pre><code>@using (Ajax.BeginForm("ParPage1", "Home", new { id = 1 }, new AjaxOptions { UpdateTargetId = "Page1Form" }, new { encType = "multipart/form-data" })) { &lt;div&gt; &lt;p&gt;This is partial view page1&lt;/p&gt; &lt;input type="button" title="" value="Button1" id="btnButton1"/&gt; &lt;p&gt;@ViewBag.Message1&lt;/p&gt; &lt;/div&gt; } &lt;script type="text/javascript"&gt; $(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) { } }); &lt;/script&gt; </code></pre> <p>Below is my ParPage2</p> <pre><code>@using (Ajax.BeginForm("ParPage2", "Home", new { id = 1 }, new AjaxOptions { UpdateTargetId = "Page2Form" }, new { encType = "multipart/form-data" })) { &lt;div&gt; &lt;p&gt;This is partial view page2&lt;/p&gt; &lt;input type="button" title="" value="Button2" id="btnButton2"/&gt; &lt;p&gt;@ViewBag.Message2&lt;/p&gt; &lt;/div&gt; } &lt;script type="text/javascript"&gt; $(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) { } }); &lt;/script&gt; </code></pre> <p>Here is my controller code</p> <pre><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"); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload