<div id="_SearchTab">
@using (Ajax.BeginForm("searchResult", "Maintenance", null,
new AjaxOptions {
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "gridArea",
HttpMethod = "Get",
LoadingElementId = "loading"
}, new { id = "searchByCriteria"}))
{
<table>
<tr>
<td>
@Html.LabelFor(model => model.UNIT_CD)
@Html.DropDownListFor(model => model.UNIT_CD, new SelectList(ViewBag.UnitList, "Value", "Text"), "-- Select a Unit --")
</td> </tr>
<tr>
<td>
@Html.LabelFor(model => model.PROGRAM_CD)
@Html.TextBoxFor(model => model.PROGRAM_CD)
</td> </tr>
</table>
<div id=" buttonHolder">
<input id="Search" type="Submit" value="Search"/>
</div>
}
</div>
<div id="gridArea">
display data here
</div>
信息搜索結果的行動:
[HttpGet]
public PartialViewResult searchResult(string unitCode, string programCode) // showed both null in debug mode
{
// typical Linq stuff, if parameters are null, show all the data in the partial view, if at leastone of the parameters is not null, use it as search criteria
}
問題是,當我提交表單,searchResult
行動的兩個參數顯示null
。我曾嘗試多種方法,包括這些如下:
<input id="Search" type="Submit" value="Search"
onclick="javascript:$('searchByCriteria').submit()" />
這(使用Javascript運行時錯誤)
<input id="Search" type="Submit" value="Search"
onclick ="@(Url.Action("searchResult", "Maintenance", new { unitCode = Model.UNIT_CD, programCode = Model.PROGRAM_CD})) "/>
並添加onsubmit=return searchForm()
到表單參數,並使用JavaScript來提交:
function searchForm() {
$("#gridArea").html("");
// Load SearchResult partialview in gridArea div
$("#gridArea").load(
'@(Url.Action("searchResult", "Maintenance", new { unitCode = Model.UNIT_CD, programCode = Model.PROGRAM_CD}))'
)
return false;
}
而jQuery的功能爲Search
按鈕的點擊事件,
$("#Search").click(
function() {
// clear the grid area
$("#gridArea").html("");
// Load SearchResult partialview in gridArea div
$("#gridArea").load(
'@(Url.Action("searchResult", "Maintenance", new { unitCode = Model.UNIT_CD, programCode = Model.PROGRAM_CD}))'
)
});
或表單的提交上面可以將參數傳遞給控制器的事件
$("#searchByCriteria").submit(function (event) {
$("#gridArea").html("");
$("#gridArea").load(
'@(Url.Action("searchResult", "Maintenance", new { unitCode = Model.UNIT_CD, programCode = Model.PROGRAM_CD}))'
)
});
無。任何人都可以幫助我嗎?
你的下拉列表被命名爲'UNIT_CD'因此參數必須是'字符串UNIT_CD'。而你沒有一個名爲'programCode'的表單控件 –
抱歉,'programCode'是一個文本框。讓我在代碼 –
中添加它然後參數需要是'字符串UNIT_CD,字符串PROGRAM_CD'(匹配窗體控件的'name'屬性 –