HOT TO USE AJAX.BEGINFORM WITH ASP.NET MVC 3 RAZOR?

Ashish Pandey

Total Post:128

Points:898
Posted by  Ashish Pandey
 981  View(s)
Ratings:
Rate this:
Is there a tutorial or code example of using Ajax.BeginForm within Asp.net MVC 3 where unobtrusive validation and Ajax exist?
  1. Norman Reedus

    Post:45

    Points:315
    Re: Hot to use Ajax.BeginForm with ASP.NET MVC 3 Razor?

    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" />

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!