我已经为此寻找解决方案 2 天了,但找不到答案,我找到的所有内容都不起作用,所以我想这里有人可能会提供帮助。
我有一个图表,我已经设法从 View 使用 Url.Action 调用,如下所示:
<img src="@Url.Action("DrawPieChart")"/>
但我不知道如何通过 Url.Action 传递参数(countryName)的值,所以我可以通过 Controller 传递它并最终在 AnalyzerData.class 中使用它
这里是 Controller :WebTrafficController
public class WebTrafficController : Controller
{
public ActionResult WebTraffic()
{
AnalzyerData analyzerData = new AnalzyerData();
//main actionResult that shows stuff not important for this question
return View(analyzerData);
}
public ActionResult DrawPieChart(string countryName)
{
AnalzyerData analyzerData = new AnalzyerData();
return PartialView(analyzerData.getChart(countryName));
}
}
类:分析器数据
public class AnalzyerData
{
public Chart chartImg { get; set; }
public Chart getChart(string countryName)
{
cWebTrafficDb checkUserStatsWrapper = new cWebTrafficDb();
checkUserStatsWrapper.cmd.CommandText = string.Format("select user_browser, count(*) from user_stats where User_country = '{0}' group by user_browser", countryName);
//checkUserStatsWrapper.cmd.CommandText = string.Format("select user_browser, count(*) from user_stats group by user_browser");
MySqlDataReader reader = checkUserStatsWrapper.cmd.ExecuteReader();
List<object> result1 = new List<object>();
List<object> result2 = new List<object>();
while (reader.Read())
{
result1.Add(reader.GetString(0));
result2.Add(reader.GetString(1));
}
chartImg = new Chart(width: 350, height: 350)
.AddTitle("Naslov")
.AddSeries(
name: "Employee",
chartType: "Pie",
xValue: result1,
yValues: result2)
.Write();
return chartImg;
}
View :Webtraffic.cshtml
@model WebTraff.Models.AnalzyerData
@{
ViewBag.Title = "WebTraffic";
}
<div class="inline">
<img src="@Url.Action("DrawPieChart","countryName", new { countryName = "Croatia" })"/>
</div>
附言如果这不可能,请告诉我该怎么做,我尝试了几种不同的方法,但我无法让它工作
最佳答案
您可以在这里尝试一件事。
替换这个:
<img src="@Url.Action("DrawPieChart","WebTraffic", new { countryName = "Croatia" })"/>
有了这个:
<img src="@Url.Action("DrawPieChart","WebTraffic", new { @countryName = "Croatia" })"/>
或者你也可以这样尝试:
@Url.Action("DrawPieChart", "WebTraffic", new { @countryName = "Croatia" })
尝试将 @
符号放在参数名称之前,并确保两边的参数名称保持相同。
希望这对您有所帮助。
关于c# - 通过 Url.Action 将参数发送到 asp.net MVC 中的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33970128/