2012-10-12 62 views
0

我可以在我的主菜單調用的普通視圖中顯示Flexigrid。我使用這個樣本http://mvc4beginner.com/Sample-Code/Insert-Update-Delete/Asp-.Net-MVC-Ajax-Insert-Update-Delete-Using-Flexigrid.html來使它工作,它對我來說工作正常。如何在ASP.NET MVC應用程序中使用flexigrid作爲局部視圖?

但是,我的想法是使用一個更復雜的界面 - 使用搜索控件定時查看並按下搜索按鈕,使用搜索到的項目數據顯示網格。

到目前爲止,我嘗試了幾件事情,不能使它工作。這裏是我嘗試過的最新的索引視圖:

@model CardNumbers.Objects.Client 

@{ 
    ViewBag.Title = "Clients"; 
} 

<h2>Clients</h2> 

<br /> 
     @using (Ajax.BeginForm("Search", "Client", 
    new AjaxOptions 
    { 
     HttpMethod = "POST", 
     InsertionMode = InsertionMode.Replace, 
     UpdateTargetId = "ClientsResults" 

    })) 
    { 
    <fieldset> 
     <legend>Search</legend> 
     <label for="clientNo">Client No: </label> 
     <input type="number" name="searchClientNo" class="numericOnly" /><br /> 
     <label for="clientName">Client Name: </label> 
     <input type = "text" size =25 data-autocomplete="@Url.Action("QuickSearch", "Client")" name ="searchClientName" /> 
     <div> 
     <input type="submit" value="Find/Refresh" />  


      @*<input type="button" value="Find/Refresh" id="ClientsSearch" data-url="@Url.Action("Client", "Client")" /> 

      @*<input type="submit" value="Find/Refresh" />*@ 
     @*  @Ajax.ActionLink("Find/Refresh", "Client", new AjaxOptions {UpdateTargetId = "ClientResults", 
      InsertionMode = InsertionMode.Replace, HttpMethod = "POST"}) *@ 
      @*}*@ 

     </div> 
    </fieldset> 
      <div style="padding-left:150px; padding-top:50px; padding-bottom:50px;" id="ClientsResults"> 
@*@{Html.RenderPartial("_Client", Model); }*@ 
       @*<table id="flexClients" style="display:none"/>*@ 


</div> 
     } 


@*<br />*@ 

你也可以在這裏看到所有的註釋嘗試。因此,在客戶端控制器搜索方法現在有這樣的代碼:

public ActionResult Search(int? searchClientNo = null, string searchClientName = null) 
     { 
      // Assume we want to select everything 
      var clients = Db.Clients; // Should set type of clients to IQueryable<Clients> 

      if ((searchClientNo ?? 0) != 0) //Number was supplied 
       clients = clients.Where(c => (c.Number == searchClientNo)); 

      // If clientNo was supplied, clients is now filtered by that. If not, it still has the full list. The following will further filter it. 
      if (!String.IsNullOrWhiteSpace(searchClientName)) // Part of the name was supplied 
       clients = clients.Where(c => (c.Name.Contains(searchClientName))); 

      return PartialView("_ClientsSearch", clients); 
      //return PartialView("_Client", clients); 
     } 

註釋的觀點是具有flexigrid局部視圖,它不工作。 _ClientsSearch視圖是使用該模板創建的「常規」索引視圖,並且可以工作。

你知道我到底錯過了什麼嗎?當我試圖將它作爲該主視圖的局部視圖使用時,flexigrid方法根本不會觸發。

回答

0

我還沒有想出我最初想到的更復雜的情況,但是我能夠使用常規視圖使其工作。這個有用的想法首先來自這個FAQ: http://code.google.com/p/flexigrid/wiki/FAQ並且看起來更接近我使用的那個樣本。

所以,現在我的客戶的看法是這樣的:

@model CardNumbers.Objects.Client 

@{ 
    ViewBag.Title = "Client"; 
} 

<form id="frmClientsSearch"> 

     <label for="clientNo">Client No: </label> 
     <input type="number" name="searchClientNo" class="numericOnly" /><br /> 
     <label for="clientName">Client Name: </label> 
     <input type = "text" size =25 value ="Please enter the search value" 
      name ="searchClientName" /> 

     <input type="button" id="btnClientsSearch" value ="Find/Refresh" />  
</form> 
<div style="padding-left: 150px; padding-top: 50px; padding-bottom: 50px;" id="ClientsResults"> 
    <table id="flexClients" style="display: none"> 
    </table> 
</div> 
<div style="display: none"> 
    <form id="sform"> 
     <input type="hidden" id="fntype" name="fntype"> 
     <input type="hidden" id="Id" name="Id"> 
     <label for="Number">Client No: </label> 
     <input type="number" id="Number" name="Number" class="numericOnly" /> 
     <label for="Name">Client Name: </label> 
     <input type="text" size="25" id="Name" name="Name" /><br /> 
     <label for="Contact11">Contact 1: </label> 
     <input type="text" size="25" id="Contact1" name="Contact1" /><br /> 
     <div class="float-right"> 
      <button type="Submit" id="btnSave">Submit</button> 
      <button type=reset id="btnCancel">Cancel</button> 
     </div> 
    </form> 
</div> 

和主要的工作是在.js文件(注意AddFormData方法)來完成:

/// <reference path = "jquery-1.5.1-vsdoc.js"/> 
/// <reference path = "jquery-ui-1.8.11.js"/> 

$(document).ready(function() { 
    $(":input[data-autocomplete]").each(function() { 
     $(this).autocomplete({ source: $(this).attr("data-autocomplete") }); 
    }); 
}); 

$(function() { 
    $('input[name="delete"]').click(function() { 
     return confirm('Are you sure?'); 
    }); 
}); 

$(".numericOnly").keypress(function (e) { 
    if (String.fromCharCode(e.keyCode).match(/[^0-9]/g)) return false; 
}); 

    $("#flexClients").flexigrid({ 
     url: '/Client/Client/', 
     dataType: 'json', 
     colModel: [ 
     { display: 'Client Id', name: 'Id', width: 100, sortable: true, align: 'center', hide: true}, 
     { display: 'Client #', name: 'Number', width: 100, sortable: true, align: 'center' }, 
     { display: 'Name', name: 'Name', width: 350, sortable: true, align: 'center' }, 
     { display: 'Contact 1', name: 'Contact1', width: 350, sortable: true, align: 'center' }, 
     ], 
     buttons: [ 
     { name: 'Add', bclass: 'add', onpress: test }, 
     { name: 'Edit', bclass: 'edit', onpress: test }, 
     { name: 'Delete', bclass: 'delete', onpress: test }, 
     { separator: true } 
     ], 
     searchitems: [ 
     { display: 'Client Name', name: 'Name' } 
     ], 
     sortname: "Name", 
     sortorder: "asc", 
     usepager: true, 
     title: 'Clients', 
     useRp: true, 
     rp: 15, 
     showTableToggleBtn: true, 
     width: 1000, 
     onSubmit: addFormData, 
     height: 300 
    }); 

//This function adds parameters to the post of flexigrid. You can add a verification as well by return to false if you don't want flexigrid to submit   
function addFormData() { 

    //passing a form object to serializeArray will get the valid data from all the objects, but, if the you pass a non-form object, you have to specify the input elements that the data will come from 
    var dt = $('#sform').serializeArray(); 
    dt = dt.concat($('#frmClientsSearch').serializeArray()); 

    $("#flexClients").flexOptions({ params: dt }); 

    return true; 
} 

$('#sform').submit(function() { 

    $('#flexClients').flexOptions({ newp: 1 }).flexReload(); 
    alert("Hello World"); 
    return false; 
}); 

function test(com, grid) { 
    if (com === 'Delete') { 
     var clientName = $('.trSelected td:eq(2)').text(); 
     if (clientName) //Variable is defined and not empty 
     { 
      if (confirm("Are you sure you want to delete " + $.trim(clientName) + "?")) 
       return false; 

      $('#fntype').val('Delete'); 
      $('#Id').val($('.trSelected td:eq(0)').text()); 
      $('#Number').val(''); 
      $('#Name').val(''); 
      $('#Contact1').val(''); 

      $('.trSelected', grid).each(function() { 
       var id = $(this).attr('id'); 
       id = id.substring(id.lastIndexOf('row') + 3); 

       addFormData(); $('#flexClients').flexOptions({ url: '/Client/Client/' }).flexReload(); 
      }); 

      clearForm(); 
     } 
    } else if (com === 'Add') { 

     $("#sform").dialog({ 
      autoOpen: false, 
      show: "blind", 
      width: 1000, 
      height: 500 
     }); 
     $("#sform").dialog("open"); 

     $('#fntype').val('Add'); 
     $('#Number').val(''); 
     $('#Name').val(''); 
     $('#Contact1').val(''); 

    } else if (com === 'Edit') { 

     $('.trSelected', grid).each(function() { 

      $("#sform").dialog({ 
       autoOpen: false, 
       show: "blind", 
       width: 1000, 
       height: 500 
      }); 
      $("#sform").dialog("open"); 

      $('#fntype').val('Edit'); 
      $('#Id').val($('.trSelected td:eq(0)').text()); 
      $('#Number').val($('.trSelected td:eq(1)').text()); 
      $('#Name').val($('.trSelected td:eq(2)').text()); 
      $('#Contact1').val($('.trSelected td:eq(3)').text()); 

     }); 

    } 
} 

function clearForm() {  

     $("#sform input").val("");  

}; 

$(function() { 
    $('#btnSave').click(function() { 
     addFormData(); 
     $('#flexClients').flexOptions({ url: '/Client/Client/' }).flexReload(); 
     clearForm(); 
     $('#sform').dialog('close'); 
     return false; 
    }); 
}); 

$(function() { 
    $('#btnCancel').click(function() {  

     // clearForm(); 
     $('#sform').dialog('close'); 
     return false; 
    }); 
}); 


$(function() { 
    $('#btnClientsSearch').click(function() { 
     addFormData(); 
     $('#flexClients').flexOptions({ url: '/Client/Client/' }).flexReload(); 
     //$.ajax({ 
     // url: $(this).data('url'), 
     // type: 'GET', 
     // cache: false, 
     // success: function (result) { 
     //  $('#ClientsResults').html(result); 
     // } 
     //}); 
     return;//false; 
    }); 
}); 

在我的客戶端的方法該控制器與之前的稍有變化相同。


現在,我的下一個挑戰是推廣上面,也不知何故,而不是調用形式sForm我發現使用更復雜的形式驗證,因爲我如果是從新建/編輯視圖。

相關問題