Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
i have a controller that returns a message but this msg show in new blank page .i want to show this msg in the same page anyone can help me..
my code is

controller is

[HttpPost]
public ActionResult NewsLetter(NewsLetterModel model)
{
try
{
string msg = "";
string res = "";


if (ModelState.IsValid)
{
kebhariBlogsEntities objEntity = new kebhariBlogsEntities();
if (model.Choice == 0)
{
res = User_Role_BL.Newsletter(model.Email, 0, model.Option, 0, 0);
msg = res;
}
else if (model.Choice == 1)
{
res = User_Role_BL.Newsletter(model.Email, 1, model.Option, 0, 0);

msg = res;
}
else
{
msg = "error";
}
}
else
{
msg = "Invalid Email";
}
return Json(msg, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
ErrorViewModel errorModel = new ErrorViewModel();
errorModel.ErrorDetails = ex.Message;
errorModel.ReturnUrl = Url.Action("Common", "NewsLetter");
return RedirectToAction("ErrorList", "Error", errorModel);
}
}

and my view page is

@model KebhariBlog.Models.CommonModel.NewsLetterModel

@* <script type="text/javascript" src="@Url.Content("~/Content//js/jquery-1.4.2.min.js")"></script>*@
@* <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>*@
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Ajax.BeginForm("NewsLetter", "Common", new AjaxOptions { UpdateTargetId = "updateNews", OnBegin = "showLoader();", OnComplete = "hideLoader();" }))
{



@Html.TextBoxFor(m => m.Email, new { @class="blog_search", @Value="" ,Placeholder="Enter Your Mail Id..." ,@style="background: none;font-size: 12px;width: 95%;border: 1px solid #CCC;",})
@Html.ValidationMessageFor(m => m.Email)

<input name="Button1" class="myButton_blog" type="submit" value="Subscribe" onclick="return validate();" style="float: right; margin-top: 5px; padding:5px 10px; font-size:12px;">





<img src="@Url.Content("~/Content/images/ajax-loader (1).gif")" id="loaderImg" style="display:none;" />
}
<script type="text/javascript">

function showLoader() {
document.getElementById('loaderImg').style.display = 'block';
}
function hideLoader() {
document.getElementById('loaderImg').style.display = 'none';
}
function validate() {
var email = document.getElementById('Email');
var emailPattern = /^[a-zA-Z0-9._-]+@@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
if (email.value == '' || !emailPattern.test(email.value)) {
document.getElementById('updateNews').innerHTML = 'Invalid Email';
return false;
}
else{
document.getElementById('updateNews').innerHTML = '';
}
}
</script>
Posted

1 solution

use 'Viewbag' element.Assign it message which you want to show & then place fallowing code in your view.

JavaScript
@if (!string.IsNullOrEmpty(ViewBag.AlertMessage))
      {
       <script type="text/javascript">
           alert("@ViewBag.AlertMessage");
           ViewBag.AlertMessage = "";
       </script>
   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900