-1

你好基本上我想要一个下拉列表来显示员工姓名列表,当管理员或管理人员正在使用它时选择图表必须显示的名称。这可能吗?如果有,请帮我...我如何获得一个下拉列表,当选择一个值来显示mvc中使用mvc图表助手的图表中的员工值时?

public ActionResult CharterColumn() 
{ 
    var results = (from c in db.Clockcards select c); 
    // the employeeid is a foreign key in the clockcards table 
    // i want to get the name from the employee table 
    // and display only that employees hours worked for the months 
    var groupedByMonth = results 
     .OrderByDescending(x => x.CaptureDate) 
     .GroupBy(x => new { x.CaptureDate.Year, x.CaptureDate.Month }).ToList(); 

    List<string> monthNames = groupedByMonth 
     .Select(a => a.FirstOrDefault().CaptureDate.ToString("MMMM")) 
     .ToList(); 

    List<double> hoursPerMonth = groupedByMonth 
     .Select(a => a.Sum(p => p.Hours)) 
     .ToList(); 

    ArrayList xValue = new ArrayList(monthNames); 
    ArrayList yValue = new ArrayList(hoursPerMonth); 

    new Chart(width: 800, height: 400, theme: ChartTheme.Yellow) 
     .AddTitle("Chart") 
     .AddSeries("Default", chartType: "Column", xValue: xValue, yValues: yValue) 
    .Write("bmp"); 
    return null; 

} 

这是我的看法

<div> 
    <img src= "@Url.Action("CharterColumn")" alt="Chart"/> 
</div> 
+0

您的代码中没有关于下拉列表的任何内容! –

+0

我在问是否可以用mvc图表做这个,如果有的话我该怎么做 – Kimberly

回答

0

你可以听的下拉列表中的change事件,阅读选择的选项值(假设它是员工id)并将其传递给action方法,该方法返回该员工记录的图表数据并更新图像标记的src属性值。

<select id="employeeList"> 
    <option value="0">None</option> 
    <option value="1">1</option> 
    <option value="2">2</option> 
</select> 
<div> 
    <img id="chart" data-url="@Url.Action("CharterColumn")" alt="Chart" /> 
</div> 

可以看到,我设置HTML5数据属性的图像标记和我的该值设定为使用Url.Action方法的动作方法的相对路径。我们将在后面的javascript读取该值。

我硬编码了SELECT元素的HTML。您可以根据需要使用Html.DropDownListHtml.DropDownListFor辅助方法,使用表中的员工数据替换它。

现在,更新您的操作方法接受值雇员ID作为参数

public ActionResult CharterColumn(int employeeId) 
{ 
    //use employeeId to filter the results 
    var results = db.Clockcards.Where(s=>s.EmployeeId==employeeId).ToList(); 
    //your existing code to return the chart 
} 

我们办理变更事件的JavaScript。

$(document).ready(function() { 

    loadChart($("#employeeList").val()); // initial load of the chart with selected option 

    $("#employeeList").change(function() { 
     var employeeId = $(this).val(); 
     loadChart(employeeId); 
    }); 

    function loadChart(employeeId) { 
     var imgSrc = $("#chart").data("url") + "?employeeId=" + employeeId; 
     $("#chart").attr("src", imgSrc); 
    } 
}); 

这应该工作,假设您的页面中没有任何其他脚本错误。

相关问题