2016-06-10 140 views
0

形式:Ajax.BeginForm無法提交下拉列表中選擇值

<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}))' 
       ) 
     }); 

無。任何人都可以幫助我嗎?

+0

你的下拉列表被命名爲'UNIT_CD'因此參數必須是'字符串UNIT_CD'。而你沒有一個名爲'programCode'的表單控件 –

+0

抱歉,'programCode'是一個文本框。讓我在代碼 –

+0

中添加它然後參數需要是'字符串UNIT_CD,字符串PROGRAM_CD'(匹配窗體控件的'name'屬性 –

回答

-4

型號。

public IList<SelectedListItem> UNIT_CD{get;set;} 

控制器:

public actionresult index() 
{ 
    Model objModel=new Model(); 
    objModel.UNIT_CD=DbContext.TableName.Select(a=>new SelectedListItem{ 
      Value=a.Value, 
      Text=a.Name 
    }); 
    return View(objModel); 
} 

MVC

@Html.LabelFor(model => model.UNIT_CD) 
      @Html.DropDownListFor(model => model.UNIT_CD, (IEnumerable<SelectListItem>)Model.UNIT_CD,"Text","Value", "-- Select a Unit --") 
+1

這是什麼都做的問題(和代碼甚至不工作!) –

+0

試試這個上面的完整代碼也許這將不會工作 – Own

+0

沒有它(和它無關的問題) –