将数据从视图传递到 dotnet 核心中的控制器
Passing data from view to controller in dotnet core
在我的项目中,我需要能够在网页上显示给定日期范围内的数据。
我的控制器是这样的:
public JiraDashboardController(IOptions<Config> app)
{
appSettings = app;
_jira = Jira.CreateRestClient(appSettings.Value.BaseUrl, appSettings.Value.UserName,appSettings.Value.Password);
_db = new UserDashboard(appSettings.Value.UserName, appSettings.Value.ProjectName, _jira);
startDate = new DateTime(2019, 01, 01); // debugging purposes
endDate = new DateTime(2019, 02, 11); // debugging purposes
}
public ActionResult Index(DateTime fromDate, DateTime toDate)
{
ViewData["OpenedIssues"] = _db.IssuesOpenedInRange(fromDate, toDate);
ViewData["ClosedIssues"] = _db.IssuesClosedInRange(fromDate, toDate);
return View();
}
以及对应的视图:
@model MyProject.Models.Jiras
@{
ViewData["Title"] = "Index";
}
<h2>Index</h2>
<h3>Total issues opened: @ViewData["OpenedIssues"]</h3>
<h3>Total issues closed: @ViewData["ClosedIssues"]</h3>
<div>
<p>From Date: <input type="text" id="fromdate"></p>
</div>
<br/>
<br/>
<div>
<p>To Date: <input type="text" id="toDate"></p>
</div>
<script>
$(function () {
$("#fromdate").datepicker();
$("#toDate").datepicker();
});
</script>
我假设我使用的是 standard jQuery date picker,但我不知道将我选择的值返回到方法中的最佳方法。
最好的方法是什么?
您可以进行如下操作:
<form asp-controller="YourControllerName" asp-action="Index" method="post">
<div>
<p>From Date: <input type="text" id="fromdate" name="fromDate"></p>
</div>
<br/>
<br/>
<div>
<p>To Date: <input type="text" id="toDate" name="toDate"></p>
</div>
<input type="submit" value="Submit" />
</form>
不要忘记在您的 Views 文件夹中添加 _ViewIports.cshtml 并添加以下代码,这会启用标签助手。
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
不要忘记为您的输入添加名称,就像我所做的那样。
模型绑定将处理将字符串转换为日期。
在我的项目中,我需要能够在网页上显示给定日期范围内的数据。
我的控制器是这样的:
public JiraDashboardController(IOptions<Config> app)
{
appSettings = app;
_jira = Jira.CreateRestClient(appSettings.Value.BaseUrl, appSettings.Value.UserName,appSettings.Value.Password);
_db = new UserDashboard(appSettings.Value.UserName, appSettings.Value.ProjectName, _jira);
startDate = new DateTime(2019, 01, 01); // debugging purposes
endDate = new DateTime(2019, 02, 11); // debugging purposes
}
public ActionResult Index(DateTime fromDate, DateTime toDate)
{
ViewData["OpenedIssues"] = _db.IssuesOpenedInRange(fromDate, toDate);
ViewData["ClosedIssues"] = _db.IssuesClosedInRange(fromDate, toDate);
return View();
}
以及对应的视图:
@model MyProject.Models.Jiras
@{
ViewData["Title"] = "Index";
}
<h2>Index</h2>
<h3>Total issues opened: @ViewData["OpenedIssues"]</h3>
<h3>Total issues closed: @ViewData["ClosedIssues"]</h3>
<div>
<p>From Date: <input type="text" id="fromdate"></p>
</div>
<br/>
<br/>
<div>
<p>To Date: <input type="text" id="toDate"></p>
</div>
<script>
$(function () {
$("#fromdate").datepicker();
$("#toDate").datepicker();
});
</script>
我假设我使用的是 standard jQuery date picker,但我不知道将我选择的值返回到方法中的最佳方法。
最好的方法是什么?
您可以进行如下操作:
<form asp-controller="YourControllerName" asp-action="Index" method="post">
<div>
<p>From Date: <input type="text" id="fromdate" name="fromDate"></p>
</div>
<br/>
<br/>
<div>
<p>To Date: <input type="text" id="toDate" name="toDate"></p>
</div>
<input type="submit" value="Submit" />
</form>
不要忘记在您的 Views 文件夹中添加 _ViewIports.cshtml 并添加以下代码,这会启用标签助手。
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
不要忘记为您的输入添加名称,就像我所做的那样。 模型绑定将处理将字符串转换为日期。