Ram
Ram

Reputation: 337

Ajax.BeginForm() in MVC4

My requirement is show the result in same page with out redirect to another page. i am doing this with Ajax.BeginForm(). but it redirect to another page. can any one help this?

My js file:

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

My View code:

 @using (Ajax.BeginForm("UserAccountInfo", "Account",  new AjaxOptions {UpdateTargetId = "result" }))
 {
@Html.ValidationSummary(true)

<div id="result"></div>

<div class="t-12-user">First Name </div>

<div> @Html.TextBoxFor(model => model.FirstName, new { @class = "user-input-bx", MaxLength = "50"})</div>

<div class="t-12-user"> Email Address </div>

<div> @Html.TextBoxFor(model => model.EmailId, new { @class = "user-input-bx", MaxLength = "50" })</div>

<div><input type="submit" value="Save"  /> </div>

    }

controller:

[HttpPost]
public ActionResult UserAccountInfo(RegisterModel Model)
{
// code
  return Content("Thank you for subscribe", "text/html");
}

web.config:

 <add key="ClientValidationEnabled" value="true" />
 <add key="UnobtrusiveJavaScriptEnabled" value="true" />

can any one help this, the success message should be show in same page..

Upvotes: 2

Views: 2994

Answers (2)

German Blanco
German Blanco

Reputation: 816

You can do this two ways...

Solution 1: You have to create a partial view inside your /Views/Account folder with your content and return it.

Controller:

[HttpPost]
public ActionResult UserAccountInfo(RegisterModel Model)
{
// code
return PartialView("_yourPartialView", model);
}

Solution2: Just return the same view and use a ViewBag variable to sent any string to the view like this...

Controller:

[HttpPost]
public ActionResult UserAccountInfo(RegisterModel Model)
{
// code
ViewBag.VariableName = "Thank you for subscribe";
return View();
}

View:

@ViewBag.VariableName

Upvotes: 0

Bappi Datta
Bappi Datta

Reputation: 1380

return as json. I think it will work for you.

Upvotes: 1

Related Questions