2013-09-26 39 views
1

我是MVC的新手,並在我的項目中使用kendo ui網格。我提出了一個問題,即在控制器參數中出現「null」,儘管數據正在通過視圖傳遞。請參閱下面的代碼。當MVC控制器通過視圖時顯示空數據

這裏是我的代碼部分來自搜索

@model IEnumerable<WeBOC.Support.Entities.Vessel> 

@{ 
ViewBag.Title = "Vessels"; 
} 

<h2>Vessels</h2> 
@(Html.Kendo().Grid(Model) 
    .Name("Grid") 
.Columns(column => 
    { 
     column.Bound(c => c.VIRNbr).Width(100).ClientTemplate(@Html.ActionLink("#=VIRNbr#", "VesselInspector", new { id = "#=VesselVisitId#" }).ToHtmlString()).Title("VIR No.").Width(150); 
     column.Bound(c => c.VesselName).Width(150).Title("Vessel Name"); 
     column.Bound(c => c.InboundVoyageNbr).Width(70).Title("IB Vyg"); 
     column.Bound(c => c.OutboundVoyageNbr).Width(70).Title("OB Vyg"); 
     column.Bound(c => c.ETA).Width(100).Title("ETA").Format("{0:dd/MM/yyyy HH:mm}"); 
     column.Bound(c => c.ArrivalDate).Width(100).Title("ATA").Format("{0:dd/MM/yyyy HH:mm}"); 
    }) 
    .Groupable() 
    .Sortable() 
    .Pageable() 
    .Filterable() 
    .DataSource(datasource => datasource 
     .Ajax() 
     .ServerOperation(true) 
     .PageSize(20) 
     .Read(read => read 
      .Action("Vessels_Read", "VesselVisit", new { id = "\\#=State\\#"}) 
      )) 
) 

這是控制器方法

public ActionResult Vessels_Read([DataSourceRequest] DataSourceRequest request, string id) 
    { 
     return Json(GetVessels(id).ToDataSourceResult(request), JsonRequestBehavior.AllowGet); 
    } 

爲什麼我得到的參數ID無效雖然通過視圖通過。

public ActionResult Vessels(string id) 
    { 
     return View(GetVessels(id)); 
    } 

    public IEnumerable<Vessel> GetVessels(string phase) 
    { 
     IEnumerable<Vessel> vsl = null; 
     vsl = this._repository.GetVesselByPhase(phase);    
     return vsl; 
    } 

任何幫助將不勝感激。

感謝, Ovais

+0

'State'是'Vessel'的成員?在'vsl = this._repository.GetVesselByPhase(phase);'上添加一個斷點,並檢查是否在第一次訪問視圖時從版本庫讀取該值。 –

+0

是的,州是船舶的成員。我已經通過放置斷點來檢查值。在第一次訪問時,我得到正確的值,但在分頁時,我變得空。 – user2542561

回答

0

如果你想將其他參數傳遞到你的動作,你應該做這樣的事情:

.DataSource(dataSource => dataSource 
    .Ajax() 
    .Read("Vessels_Read", "VesselVisit").Data("getState") //getState is a javascript function 
) 

然後添加一個腳本塊:

<script> 
function getState() { 
    return { 
     ID: // your ID, obtained with jQuery, from the View Model, hardcoded, etc... 
    }; 
} 
</script> 

更多info here

編輯

我有這個權利,現在的工作。我正在使用視圖模型,但它不應該有任何不同。

查看:

.DataSource(dataSource => dataSource 
    .Ajax() 
    .Read("MY_ACTION", "MY_CONTROLLER", new { Id = Model.Id }) 
) 

控制器:

[HttpPost] 
public ActionResult MY_ACTION([DataSourceRequest] DataSourceRequest request, long Id) 

即使我改變new { Id = Model.Id }new { Id = "QWERTY" }和操作參數來串,我收到"QUERTY"作爲值。

+0

我已經試過這個,但沒有結果..! – user2542561

+0

'.Read(讀取=>讀取 .Action( 「Vessels_Read」, 「VesselVisit」) 。數據( 「getvessels」) )'' – user2542561

+0

' – user2542561

相關問題