2014-09-26 98 views
0

我有三個dropdownlist - Project,Sprint,Story。 Sprint下拉列表將根據選定的項目綁定,故事下拉列表將根據選定的Sprint綁定。在選定的故事的基礎上,我想展示一個webgrid。多個局部視圖渲染

我在做什麼是: 我的項目dropdownlist是在主視圖頁面上,Sprint dropdownlist和Story dropdownlist是兩個不同的局部視圖。當我從項目中選擇,選定的值在jquery中,並傳遞給控制器​​:

$('#Project').change(function (e) { 
    e.preventDefault(); 
    var selectedVal = $("#Project").val(); 
    $.ajax({ 
     url: "/Task/BindSprintList", 
     data: { projectTitle: selectedVal }, 
     type: 'Get', 
     success: function (result) { 
       $('#ViewGrid').html(result); 
     }, 
     error: function() { 
      alert("something seems wrong"); 
     } 
    }); 
}); 

現在衝刺下拉列表出現。當我來自Sprint選擇,選擇的值被取入jQuery和傳遞給作爲控制器:

$('#Sprint').change(function (e) { 
    e.preventDefault(); 
    var selectedProjectVal = $("#Project").val(); 
    var selectedSprintVal = $("#Sprint").val(); 
    $.ajax({ 
     url: "/Task/BindStoryList",    
     data: { projectTitle: selectedProjectVal, sprintTitle: selectedSprintVal }, 
     type: 'Get', 
     success: function (result) { 
      $('#ddlStory').html(result);   }, 
     error: function (err) { 
      alert("something seems wrong "+ err); 
     } 
    }); 
}); 

但現在故事DROPDOWNLIST沒有顯示。

MainPage.cshtml

<table> 
    @{ 
     if (ViewBag.ProjectList != null) 
     { 
      <tr> 
       <td> 
        <h4>SELECT PROJECT&nbsp; &nbsp; &nbsp; &nbsp;</h4> 
       </td> 
       <td> 
        @Html.DropDownList("Project", new SelectList(ViewBag.ProjectList, "Value", "Text"), " -- Select -- ") 
       </td> 
      </tr> 
     } 
     if (ViewBag.SprintList != null) 
     { 
      Html.RenderPartial("PartialSprintDropDown", Model.AsEnumerable()); 
     } 
     if (ViewBag.StoryList != null) 
     { 
      Html.RenderPartial("PartialStoryDropDown", Model.AsEnumerable()); 
     } 
    } 
</table> 

PartialSprintDropDown.cshtml

<table> 
    <tr> 
     <td> 
      <h4>SELECT SPRINT</h4> 
     </td> 
     <td> 
      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@Html.DropDownList("Sprint", new SelectList(ViewBag.SprintList, "Value", "Text"), " -- Select -- ") 
     </td> 
    </tr>  
</table> 
<script src="~/Script/Task/IndexTask.js" type="text/javascript"></script> 

PartialStoryDropDown.cshtml

<div id="ddlStory"> 
    <table> 
     <tr> 
      <td> 
       <h4>SELECT STORY</h4> 
      </td> 
      <td> 
       &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@Html.DropDownList("Story", new SelectList(ViewBag.StoryList, "Value", "Text"), " -- Select -- ") 
      </td> 
     </tr>  
    </table> 
</div> 
<script src="~/Script/Task/IndexTask.js" type="text/javascript"></script> 

任何人都可以建議我爲什麼Story DropdownList不顯示。即使當我貶低PartialStoryDropDown.cshtml,「ViewBag.StoryList」包含數據按預期方式,但不顯示在頁面上。

我在Viewbag.SprintList和Viewbag.StoryList中包含我的數據。 正在顯示SprintDropdownlist。

如何解決這個問題?

BindSprintList()

public ActionResult BindSprintList(string projectTitle) 
     { 
      try 
      { 
       string Owner = Session["UserName"].ToString(); 

       int? ProjectId = GetProjectID(projectTitle); 

       var ddlSprint = new List<string>(); 

       List<SelectListItem> items = new List<SelectListItem>(); 

       var querySprint = (from sp in entities.Sprints 
            where sp.Project_ID == ProjectId && sp.Sprint_Status != "Completed" 
            select sp).ToList(); 

       foreach (var item in querySprint.ToList()) 
       { 
        SelectListItem li = new SelectListItem 
        { 
         Value = item.Sprint_Title, 
         Text = item.Sprint_Title 
        }; 
        items.Add(li); 
       } 

       IEnumerable<SelectListItem> List = items; 

       ViewBag.SprintList = new SelectList(List, "Value", "Text"); 
      } 
      catch (Exception e) 
      { 
       var sw = new System.IO.StreamWriter(filename, true); 
       sw.WriteLine("Date :: " + DateTime.Now.ToString()); 
       sw.WriteLine("Location :: AgileMVC >> Controllers >> TaskController.cs >> public ActionResult BindSprintList(string projectTitle)"); 
       sw.WriteLine("Message :: " + e.Message); 
       sw.WriteLine(Environment.NewLine); 
       sw.Close(); 
      } 

      return PartialView("PartialSprintDropDown", ViewBag.SprintList); 
     } 

BindStoryList()

public ActionResult BindStoryList(string projectTitle, string sprintTitle) 
    { 
     try 
     { 
      string Owner = Session["UserName"].ToString(); 

      int? ProjectId = GetProjectID(projectTitle); 

      int? SprintId = GetSprintID(ProjectId, sprintTitle); 

      var ddlStory = new List<string>(); 

      List<SelectListItem> items = new List<SelectListItem>(); 

      var queryStory = (from st in entities.Stories 
           join spss in entities.SprintStories on st.Story_ID equals spss.Story_ID 
           where spss.Sprint_ID == SprintId && spss.Project_ID == ProjectId 
           select st).ToList(); 

      foreach (var item in queryStory.ToList()) 
      { 
       SelectListItem li = new SelectListItem 
       { 
        Value = item.Story_Title, 
        Text = item.Story_Title 
       }; 
       items.Add(li); 
      } 

      IEnumerable<SelectListItem> List = items; 

      ViewBag.StoryList = new SelectList(List, "Value", "Text"); 
     } 
     catch (Exception e) 
     { 
      var sw = new System.IO.StreamWriter(filename, true); 
      sw.WriteLine("Date :: " + DateTime.Now.ToString()); 
      sw.WriteLine("Location :: AgileMVC >> Controllers >> TaskController.cs >> public ActionResult BindStoryList()"); 
      sw.WriteLine("Message :: " + e.Message); 
      sw.WriteLine(Environment.NewLine); 
      sw.Close(); 
     } 

     return PartialView("PartialStoryDropDown", ViewBag.StoryList); 
    } 
+0

介意分享您的BindStoryList和BindSprintList操作? – 2014-09-26 04:27:36

+0

它看起來像你試圖在下拉菜單中顯示一個'table'(用'$('#ddlStory')'和你的部分視圖的html。 – Mrchief 2014-09-26 04:27:54

+0

其實@Mrchief ...我認爲ishud把PartialStoryDropDown內容放在div tag wid ID「ddlStory」,這就是爲什麼im顯示那一個... – Mayank 2014-09-26 04:32:53

回答

0

我想我看到你的問題 - ddlStory DIV來作爲結果的一部分。所以當你說$('#ddlStory')時,它不會做任何事情,因爲它不在頁面上。

我認爲在您的主頁中,您需要佔位符爲ddlStory並替換佔位符的html。類似$('#ddlStoryWrapper').html(result)其中ddlStoryWrapper只是頁面上的空白div。

+0

不要忘記upvote和標記爲答案! – Mrchief 2014-09-26 05:02:43