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