我正在查看不同的帖子,以便点击按钮可以执行两项操作。
我的aspx页面包含表单,按钮点击它会将表单提交给控制器,我在我的控制器中使用表单数据,并根据我的表单中的值我渲染部分视图,我想显示我的局部视图我的aspx。
我正在做以下步骤:
<form id="StatsForm" name="StatsForm" action="../Stats/Index/" method="POST"
enctype="multipart/form-data">
<%= Html.AntiForgeryToken()%>
<% Html.RenderPartial("OptionalFields"); %>
</form>
<script type="text/javascript" language="javascript">
//<![CDATA[
$(document).ready(function () {
$("#GetReport").click(function () {
$("form[name=StatsForm]").submit();
});
});
//]]>
</script>
我的控制器:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection form)
{
// Deal with the form
var strId= Convert.ToInt32(form["manufacturerId"]);
var str1Id= Convert.ToInt32(form["reportId"]);
var str2Id= Convert.ToInt32(form["categoryId"]);
var str3Id= Convert.ToInt32(form["retailerId"]);
var str4Id= Convert.ToInt32(form["countryId"]);
var str5Id= Convert.ToInt32(form["regionId"]);
var str6Id= (form["ManufacturerWidgetId"]);
var startDate = new DateTime(1, 1, 1, 0, 0, 0, 0);
var endDate = new DateTime(1, 1, 1, 0, 0, 0, 0);
if (!String.IsNullOrEmpty(form["StartDate"]))
{
startDate = Convert.ToDateTime(form["StartDate"]);
}
if (!String.IsNullOrEmpty(form["EndDate"]))
{
endDate = Convert.ToDateTime(form["EndDate"]);
}
var reportName = _reportRepository.GetReport(reportId);
var stats = new Stats
{
};
switch (reportName.Code)
{
case "ABC":
return RedirectToAction("InterStats",
new
{
});
break;
case "XYZ":
return RedirectToAction("ParametersCumLeads",
new
{
});
break;
case "IMP":
break;
}
我的部分观点:
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public ActionResult InterStats(int str1Id, int str2Id, DateTime startDate, DateTime endDate)
{
var str = _manufacturerWidgetsRepository.GetManufacturerWidgetByManufacturerAndCountry(manufacturerId, countryId);
var report = new ABCReport();
var reportList = new List<ReportList>(); // a list of my anonymous type without the relationships
report.reportList = new List<Record>();
var count = 1;
foreach (var mw in str)
{
// I am doing some function
// Create the data for the report
}
return PartialView(report);
}
我试图在我的aspx中显示我的局部视图。 我的问题是没有点击我的提交按钮两次如何将表单提交给我的控制器,并在我的aspx中注入我的部分视图。
答案 0 :(得分:1)
确定,
要实现这一目标,您需要做两件事:
PartialView
,它委托给你在InterStats
中调用的部分),而不是RedirectToAction
< / LI>
.submit()
操作需要重构才能通过ajax
提交表单,然后接受成功回调中返回的ActionResult
并将其附加到您的网页上。我在途中,否则会做一个简单的例子。如果仍然需要,将在稍后更新。