JQGrid添加新记录

时间:2015-02-03 10:35:41

标签: jquery asp.net-mvc c#-4.0 jqgrid

我有一个JQGrid,它在联系人编辑表单上保存与该联系人关联的备注。我正在尝试使Add New Note功能正常工作,但我无法将ContactId传递给控制器​​。 ContactIdEdit中存在Delete,它们似乎工作得很好。 Note传递给Add New的控制器,但不传递给ContactId

查看JQuery

  $("#JQGrid1").jqGrid({
    url: '@Url.Action("GetNotes", "Contact")',
    editurl: '@Url.Action("EditNote", "Contact")',
    mtype: "GET",
    datatype: "json",
    page: 1,
    postData: { id: document.getElementById('SelectedContact_ContactID').value },
    jsonReader: { id: document.getElementById('SelectedContact_ContactID').value },
    prmNames: { id: document.getElementById('SelectedContact_ContactID').value },
    colNames: ["Id", "ContactId", "Note", "Date Created", "Created By"],
    colModel: [
        { editable: true, key: true, width: 50, name: "ID", hidden: true },
        { editable: true, width: 60, name: "ContactId", hidden: true },
        { editable: true, width: 460, name: "Note", hidden: false },
        {
            editable: false,
            width: 160,
            name: "DateCreated",
            formatter: "date",
            formatoptions: { srcformat: "m/d/Y h:i:s A", newformat: "y-m-d" },
            hidden: false
        },
        { editable: false, width: 160, name: "CreatedBy", hidden: false },
    ],
    height: "auto",
    caption: "Notes",
    rowNum: 5,
    pager: "#JQGrid1_pager",
    loadComplete: function() {
        //alert("OK");
    },
    loadError: function(jqXHR, textStatus, errorThrown) {
        alert('HTTP status code: ' + jqXHR.status + "\n" +
            'textStatus: ' + textStatus + "\n" +
            'errorThrown: ' + errorThrown);
        alert('HTTP message body (jqXHR.responseText): ' + "\n" + jqXHR.responseText);
    },
}).jqGrid('navGrid', '#JQGrid1_pager', { edit: true, add: true, del: true, search: false, view: false, refresh: true },
{
    // edit options
    closeAfterEdit: true,
},
{
    //add options
    closeAfterAdd: true,
},
{
    // del options

});

控制器代码

[HttpPost]
    public ActionResult EditNote(string oper, int? ID, int ContactId, string Note)
    {
        switch (oper)
        {
            case "add":
            {
                ContactNote cn = new ContactNote
                {
                    ContactId = (int)ContactId,
                    Note = Note,
                    DateCreated = DateTime.Now,
                    CreatedBy = User.Identity.Name
                };
                _contactNoteRepository.Upsert(cn);
                return Content("true");
            }
            case "del":
                _contactNoteRepository.Delete((int)ID);
                return Content("true");
            case "edit":
            {
                ContactNote cn = new ContactNote
                {
                    ID = (int)ID,
                    ContactId = (int)ContactId,
                    Note = Note,
                    DateCreated = DateTime.Now,
                    CreatedBy = User.Identity.Name
                };
                _contactNoteRepository.Upsert(cn);
                return Content("true");
            }
        }

        return Content("false");
    }

1 个答案:

答案 0 :(得分:1)

我找到了解决方案

editoptions: {defaultValue:'aValue'}

所以我编辑了这行

{ editable: true, width: 60, name: "ContactId", hidden: true },

{ editable: true, width: 60, name: "ContactId", hidden: true, editoptions: {defaultValue:document.getElementById('SelectedContact_ContactID').value} },

并在表单中填充了ContactId,并将其传递给控制器​​方法。