如何掌握和细节MVC 2 + Jqgrid

时间:2011-11-30 01:31:37

标签: asp.net-mvc-2 jqgrid

方案: 假设以下类

public class remate
{       
    public virtual int id { get; set; }
    public virtual DateTime fecha { get; set; }
    ...
    public virtual List<detalle_remate> detalle_remate { get; set; }
    ...
}

public class detalle_remate
{
    public virtual int id { get; set; }
    public virtual remate remate { get; set; }
    public virtual cliente cliente_comprador { get; set; }
    public virtual cliente cliente_vendedor { get; set; }
    ...
}

对于该类的每一行重新映射,有n行detalle_remate。 用户通过首先选择客户买家(主人)的数量来输入数据,可能是同一买家的多行detalle_remate

和这个控制器

public class RemateController : Controller
{     
    private remateRepository Repo = new remateRepository();
    private remate entity;
    private List<detalle_remate> lineas = new List<detalle_remate>();
    public ActionResult Crear()
    {
        entity = new remate(); //crear nueva instancia
        entity.fecha = DateTime.Today;
        ...
        return View(entity);
    }

    public ActionResult List(string sidx, string sord, int page, int rows)
    {
        int pageIndex = Convert.ToInt32(page - 1);
        int rowIndex = Convert.ToInt32(rows);
        var datos = lineas.OrderBy(x => x.id).Skip(pageIndex * rowIndex).Take(rowIndex);
        double totalregistros = lineas.Count();
        int totalPages = (int)Math.Ceiling((decimal)totalregistros / (decimal)rowIndex);
        var data = new
        {
            total = totalPages,
            page = page,
            records = totalregistros,
            rows = from a in datos
                   select new
                   {
                       cell = new string[] {
                           a.id.ToString(),
                           a.cliente_vendedor.razon_social,
                           ...
                       }
                   }
        };
        return Json(data, JsonRequestBehavior.AllowGet);
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult ABM(detalle_remate entity, FormCollection formCollection)
    {
...
    }
}
}

设计视图Crear如下

<% using (Html.BeginForm())
   {%>  
   <%= Html.ValidationSummary(true) %> 
   <fieldset> 
   <%=Html.LabelFor(m => m.id) %><%=Html.EditorFor(m => m.id) %> 
   <%=Html.ValidationMessageFor(m => m.id) %> <br />
   <%=Html.LabelFor(m => m.fecha) %><%=Html.EditorFor(m => m.fecha) %> 
   <%=Html.ValidationMessageFor(m => m.fecha) %><br />
   <%=Html.Label("Comprador:") %><%=Html.DropDownList("cliente_comprador", (SelectList)ViewData["listaclientes"])%> <br />
   <table id="list" class="scroll" cellpadding="0" cellspacing="0"></table>
   <div id="listPager" class="scroll" style="text-align:center;"></div>
   <div id="listPsetcols" class="scroll" style="text-align:center;"></div>  
   <script type="text/javascript">
   $(document).ready(function() {
    var updateDialog = {
        url: '<%= Url.Action("ABM", "Remate") %>'
            , closeAfterAdd: true
            , closeAfterEdit: true
            , modal: true
            , onclickSubmit: function(params) {
                var ajaxData = {};
                var list = $("#list");
                var selectedRow = list.getGridParam("selrow");
                rowData = list.getRowData(selectedRow);
                ajaxData = { id: rowData.id };
                return ajaxData;
            }
            , width: "700"
    };
    $.jgrid.nav.addtext = "Agregar";
    $.jgrid.nav.edittext = "Editar";
    $.jgrid.nav.deltext = "Borrar";
    $("#list").jqGrid({
        url: '<%= Url.Action("List", "Remate") %>',
        datatype: 'json',
        mtype: 'GET',
        colNames: ['Número', 'Vendedor', 'Especie', ...],
        colModel: [
                { name: 'id', index: 'id', width: 40, align: 'left', editable: true, editrules: { edithidden: false }, hidedlg: true, hidden: true },
                { name: 'cliente_vendedor', index: 'cliente_vendedor', width: 200, align: 'left', editable: true, edittype: 'select', editoptions: { dataUrl: "/Cliente/ClienteSelect" }, editrules: { required: true }, formoptions: { elmsuffix: ' *'} },
                { name: 'especie', index: 'especie', width: 70, align: 'left', editable: true, edittype: 'select', editoptions: { dataUrl: "/Especie/EspecieSelect" }, editrules: { required: true }, formoptions: { elmsuffix: ' *'} },...                
                ],
        pager: $('#listPager'),
        rowNum: 20,
        rowList: [5, 10, 20],
        sortname: 'id',
        sortorder: "desc",
        viewrecords: true,
        imgpath: '/Content/ui-lightness/Images',
        width: "850",
        height: "250",
        ondblClickRow: function(rowid, iRow, iCol, e) {
            $("#list").editGridRow(rowid, prmGridDialog);
        }
    }).navGrid('#listPager',
            {
                edit: true, add: true, del: true, search: false, refresh: true
            },
            updateDialog,
            updateDialog,
            updateDialog
        );
});       
</script>
<% } %>
</fieldset>  

正如您所看到的,应该在信息存储在jdgrid中时发送Comprador。我该如何添加这些信息? *将JSON作为收入发送给Comprador而不在网格中显示它?

任何人都可以通过一些例子来帮助我吗?

1 个答案:

答案 0 :(得分:0)

将此ajaxData = { id: rowData.id };更改为此ajaxData = { cliente_comprador: $("#cliente_comprador").val() };时工作正常。

cliente_comprador包含在ActionResult ABM上的formCollection上