Question

I am trying to save or insert data into my database where my table is Purchase. But it is not saving any data to my table, and also not showing any error. At first i don't know why Save button is not clicking. Here is my ajax method where i am posting to controller for saving data. here is my ajax code. Please Help me..

 $('#btnSave').click(function () {
                 var pform = $('frmpm');
                 var llink = '/Purchases/PurchaseMaster/';
                 var pm = {};
                 $.each(pform.serializeArray(), function () {
                     pm[this.name] = this.value;
                 });
                 $.ajax({
                     url: llink,
                     async: false,
                     cache: false,
                     type: 'POST',
                     data: pm,
                     success: function (data) { },
                     error: function (data) {
                         alert(data.msg);
                     }
                 });
             });

Here is my Controller Where is I am Trying to save data...it is not showing any error or success message..

[HttpPost]
        public ActionResult PurchaseMaster(Purchase pm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Insert("Purchase", "PId", pm);
                    return Json(new { msg = "Purchase has been saved Successfully." });
                }
                return Json(new { msg = "Purchase Is InValid." });
            }
            catch
            {
                return Json(new { msg = "Sorry ! Something wrong with your Purchase Master." });
            }
        }

And this is my index view

  @using (Html.BeginForm("Index", "Purchases", FormMethod.Post, new { @id="frmpm"}))
{
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Purchase</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.PId)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.PId)
            @Html.ValidationMessageFor(model => model.PId)
        </div>

        <div class="editor-label">
            @Html.Label("Purchase No")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.PNo)
            @Html.ValidationMessageFor(model => model.PNo)
        </div>

        <div class="editor-label">
            @Html.Label("Purchase Date")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.PDate)
            @Html.ValidationMessageFor(model => model.PDate)
        </div>

        <div class="editor-label">
            @Html.Label("SuppLier")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.SupId)
            @Html.ValidationMessageFor(model => model.SupId)
        </div>

        <div class="editor-label">
            @Html.Label("Supply Area")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.SupArea)
            @Html.ValidationMessageFor(model => model.SupArea)
        </div>

        <div class="editor-label">
            @Html.Label("Purchase Amount")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.PAmount)
            @Html.ValidationMessageFor(model => model.PAmount)
        </div>

        <div class="editor-label">
            @Html.Label("Purchase Mode")
        </div>
        <div class="editor-field">
            @Html.DropDownList("PMode")
            @Html.ValidationMessageFor(model => model.PMode)
        </div>

        <div class="editor-label">
            @Html.Label("Cheque No")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ChqNo)
            @Html.ValidationMessageFor(model => model.ChqNo)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.BankAcc)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.BankAcc)
            @Html.ValidationMessageFor(model => model.BankAcc)
        </div>
         <div id="splist" >
        </div>
        @Html.Action("Purchaseitem")
         <p>
            <input id="btnSave" type="button" value="Save" />
        </p>
    </fieldset>
}

I think Problem Is here Cause it is not executing..

 db.Insert("Purchase", "PId", pm);
Was it helpful?

Solution

You need to precisely locate where is the problem: in javascript or in the controller.

Are you set breakpoints on lines:

  1. IF (ModelState.IsValid)
  2. Db.Insert ("Purchase", "PId", pm); ?

Is code execution comes to this breakpoints or problem completely in javascript ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top