Wednesday, January 21, 2015

Convert @HTML.BeginForm to @Ajax form without using @Ajaxform helper

1.  In Test Controller
public ActionResult AjaxForm()
        {
            return View();
        }
 
 
2 . <h2>AjaxForm</h2>
 
@using (Html.BeginForm("AjaxForm""Test"nullFormMethod.Post, new { @id = "myform" }))
{
    @:CategoryName @Html.TextBox("CategoryName")<br />
 
    @:Description @Html.TextBox("Description")
 
    <input type="submit" value="Submit" />
}
 
<div id="divResult">
 
</div>
 
@*//Now we convert this simple @HTML.BeginForm form posting  to Ajax form using JQuery without using @Ajaxform helper*@
 
<script type="text/javascript">
    $(function () {
        $("#myform").on("submit"function(e){
            e.preventDefault();
 
            $.ajax({
                url: this.action,
               type: this.method,
               data: $(this).serialize(),
               
               success: function (data) {
                   console.log(data);
                   $("#divResult").html(data);
 
               }
            });
 
        });
    });
 
</script> 

3.
[HttpPost]
        public ActionResult AjaxForm(FormCollection frm)
        {
            
            ViewBag.CategoryName = frm["CategoryName"].ToString();
            ViewBag.Description = frm["Description"].ToString();
            return PartialView("_AjaxformPartial");
 
            //Now we convert this simple @HTML.BeginForm form posting  to Ajax form using JQuery without using @Ajaxform helper
        }


 4.
_AjaxformPartial.cshtml
 
<h2> @ViewBag.CategoryName </h2>
<br />
<h3>@ViewBag.Description</h3>

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More