Home > DeveloperSection > Forums > Hot to use Ajax.BeginForm with ASP.NET MVC 3 Razor?
Ashish Pandey

Total Post:128

Points:898
Posted on    October-29-2014 11:34 PM

 ASP.NET MVC ASP.NET MVC  Mvc4 
Ratings:


 1 Reply(s)
 659  View(s)
Rate this:
Is there a tutorial or code example of using Ajax.BeginForm within Asp.net MVC 3 where unobtrusive validation and Ajax exist?


Norman Reedus
Norman Reedus

Total Post:45

Points:315
Posted on    October-29-2014 11:38 PM

Model:

public class MyViewModel

{

     [Required]

     public string Foo { get; set; }

}

Controller:

public class HomeController : Controller

{

     public ActionResult Index()

     {

          return View(new MyViewModel());

     }

 

     [HttpPost]

      public ActionResult Index(MyViewModel model)

      {

           return Content("Thanks", "text/html");

      }

 }

View:

@model AppName.Models.MyViewModel

 

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

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

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

 

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

 

@using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "result" }))

{

    @Html.EditorFor(x => x.Foo)

    @Html.ValidationMessageFor(x => x.Foo)

    <input type="submit" value="OK" />

}


Don't want to miss updates? Please click the below button!

Follow MindStick