将数据保存到数据库有什么帮助吗?

时间:2014-02-25 06:15:58

标签: ajax database asp.net-mvc-4 post petapoco

我正在尝试将数据保存或插入我的数据库,其中我的表是Purchase。但它没有将任何数据保存到我的表中,也没有显示任何错误。起初我不知道为什么保存按钮没有点击。这是我的ajax方法,我将其发布到控制器以保存数据。这是我的ajax代码。请帮助我..

 $('#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);
                     }
                 });
             });

这是我的控制器我在哪里试图保存数据...它没有显示任何错误或成功消息..

[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." });
            }
        }

这是我的索引视图

  @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>
}

我认为问题在这里因为它没有执行..

 db.Insert("Purchase", "PId", pm);

1 个答案:

答案 0 :(得分:0)

您需要精确定位问题所在:在javascript或控制器中。

您是否在行上设置了断点:

  1. IF (ModelState.IsValid)
  2. Db.Insert ("Purchase", "PId", pm);
  3. 代码执行是否在javascript中完全出现在此断点或问题中?