2014-09-26 155 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