MVC 5 DIV 充当小部件(使用 Url.Action)以 post 形成值
MVC 5 DIV acting a widget (using Url.Action) to post form values
我现在有点不知所措,要设置场景,页面顶部有一个下拉列表,后面跟着一堆 "clickable" DIV 元素和我通过在它们周围包裹一个 Action 使它们可以点击。
但是,问题是其中 none 个 POST 值给控制器,所以我无法读取下拉列表的内容。进一步阅读表明这是因为我使用的 Action 通常没有 post 值。
但是,到目前为止我发现的所有示例似乎都包含一个 "submit" 输入类型按钮,但我更希望 DIV 元素既可以充当按钮又可以充当按钮 post 值。
我想问题是,在我放弃我正在尝试做的事情之前 - 这样的事情可能吗?任何帮助表示赞赏!
表单内(下拉)
<div class="row">
<span class="label label-primary">Reporting Period </span> @Html.DropDownListFor(x => x.ReportQuarterID, new SelectList(Model.ReportingQuarters, "Id", "Name", new { @class = "dropdown" }), new { @id = "ddlReportingQuarter" })
</div>
表单内(可点击Div)
<a href="@Url.Action("QuarterlySpendBySite", "Reports")">
<div class="col-lg-4 col-md-6 col-sm-12 col-xs-12 hvr-grow">
<div class="ibox float-e-margins">
<div class="ibox-title y-bgc-s1">
<h5>Quarterly Spend By Site</h5>
</div>
<div class="ibox-content no-padding">
</div>
</div>
</div>
</a>
控制器内
[HttpPost]
public ActionResult QuarterlySpendBySite()
{
return View(new Test.Models.TestReporting_BL());
}
以上例程被忽略,如果我删除 HTTPPost 它会正常工作但如果我尝试 Request.Form["ddlReportingQuarter"] - 它 returns null.
<script type="text/javascript">
$(function() {
$('#ActionLinkId').click(function() {
var selectedVal=$("#ddlReportingQuarter option:selected").val();
$.post({
url: $('#ActionLinkId').attr('href'),
type: 'post',
data: {selectVal: selectedVal}
success: function(data) {
}
});
// prevent the default action, If not it'll redirect
return false;
});
}
</script>
并在控制器中进行此更改。
[HttpPost]
public ActionResult QuarterlySpendBySite(string selectVal)
{
return View(new Test.Models.TestReporting_BL());
}
我现在有点不知所措,要设置场景,页面顶部有一个下拉列表,后面跟着一堆 "clickable" DIV 元素和我通过在它们周围包裹一个 Action 使它们可以点击。
但是,问题是其中 none 个 POST 值给控制器,所以我无法读取下拉列表的内容。进一步阅读表明这是因为我使用的 Action 通常没有 post 值。
但是,到目前为止我发现的所有示例似乎都包含一个 "submit" 输入类型按钮,但我更希望 DIV 元素既可以充当按钮又可以充当按钮 post 值。
我想问题是,在我放弃我正在尝试做的事情之前 - 这样的事情可能吗?任何帮助表示赞赏!
表单内(下拉)
<div class="row">
<span class="label label-primary">Reporting Period </span> @Html.DropDownListFor(x => x.ReportQuarterID, new SelectList(Model.ReportingQuarters, "Id", "Name", new { @class = "dropdown" }), new { @id = "ddlReportingQuarter" })
</div>
表单内(可点击Div)
<a href="@Url.Action("QuarterlySpendBySite", "Reports")">
<div class="col-lg-4 col-md-6 col-sm-12 col-xs-12 hvr-grow">
<div class="ibox float-e-margins">
<div class="ibox-title y-bgc-s1">
<h5>Quarterly Spend By Site</h5>
</div>
<div class="ibox-content no-padding">
</div>
</div>
</div>
</a>
控制器内
[HttpPost]
public ActionResult QuarterlySpendBySite()
{
return View(new Test.Models.TestReporting_BL());
}
以上例程被忽略,如果我删除 HTTPPost 它会正常工作但如果我尝试 Request.Form["ddlReportingQuarter"] - 它 returns null.
<script type="text/javascript">
$(function() {
$('#ActionLinkId').click(function() {
var selectedVal=$("#ddlReportingQuarter option:selected").val();
$.post({
url: $('#ActionLinkId').attr('href'),
type: 'post',
data: {selectVal: selectedVal}
success: function(data) {
}
});
// prevent the default action, If not it'll redirect
return false;
});
}
</script>
并在控制器中进行此更改。
[HttpPost]
public ActionResult QuarterlySpendBySite(string selectVal)
{
return View(new Test.Models.TestReporting_BL());
}