Hot to use Ajax.BeginForm with ASP.NET MVC 3 Razor?

Total Post:128

Points:898
 1308  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. 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" />
    }

      Modified On Apr-06-2018 01:41:01 AM

Answer

NEWSLETTER

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