Wednesday, November 1, 2017

Form submit using jquery



//Scripts---------------------------------
    $(function () {

        $('#form_Customer').submit(function (event) {
            event.preventDefault();         
           var Postdata = JSON.stringify(GetFormDataAsJson("#form_Customer"));
            $.ajax({
                type: "POST",
                dataType: 'json',
                contentType: "application/json; charset=utf-8",
                url: 'Home/Customer',
                data: Postdata,
                async: false,
                success: function (data) {
                    alert("Saved Sucessfully", "Done");
                },
                error: function (data) {

                    alert(data.responseText + "Fail", "Failed");
                }

            });

        });
    });

    function GetFormDataAsJson(frm) {
        var formObject = $(frm).serializeArray();
        var data = {};
        $.each(formObject, function (index, ele) {
            var groupIds = [];
            var groupobjects = $.map(formObject, function (obj, index) {
                if (obj.name == ele.name) {
                    groupIds.push(obj.value);
                    return obj;
                }
            });
            if (groupobjects.length > 1 && groupIds.length > 1) {
                data[ele.name] = groupIds;
            }
            else {
                data[ele.name] = ele.value;
            }
        });
        return data;
    }
==============================
  public class CoustomerModel {
        public int id { get; set; }
        public string Name { get; set; }
        public int Amount { get; set; }
 
    }

================================
@using MyMVC.Models
@model CoustomerModel

@using (Html.BeginForm("Customer", "Home", FormMethod.Post, new { id = "form_Customer", enctype = "multipart/form-data" }))
{
   

       
   
        @Html.TextBoxFor(m => m.id, new { @id="txtId",@placeholder="Customer Id",@class="form-control"})
   
   

       
        @Html.TextBoxFor(m => m.Name, new { @id="txtName",@placeholder="Customer Name",@class="form-control"})     
   

   

       
        @Html.TextBoxFor(m => m.Amount, new { @id="txtAmount",@placeholder="Customer Amount",@class="form-control"})   
   
        (input id="btnSave" type="submit" value="save" class="btn btn-primary" /)
}
================================
  [HttpPost]
        public ActionResult Customer(CoustomerModel obj)
        {
            return View();
        }



1 comment:

javascript Filter/index off

 var family = [{"name":"Jack",  "age": 26},               {"name":"Jill",  "age"...