C# 在单击按钮时渲染动态局部视图
我在看不同的帖子,所以点击按钮我们可以做两个动作 我的aspx页面包含表单,单击按钮将表单提交给控制器,我在控制器中使用表单数据,并根据表单中的值呈现部分视图,我希望在我的aspx中显示部分视图 我正在执行以下步骤:C# 在单击按钮时渲染动态局部视图,c#,javascript,asp.net-mvc-3,C#,Javascript,Asp.net Mvc 3,我在看不同的帖子,所以点击按钮我们可以做两个动作 我的aspx页面包含表单,单击按钮将表单提交给控制器,我在控制器中使用表单数据,并根据表单中的值呈现部分视图,我希望在我的aspx中显示部分视图 我正在执行以下步骤: <form id="StatsForm" name="StatsForm" action="../Stats/Index/" method="POST" enctype="multipart/form-data">
<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.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);
}
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
公共操作结果状态间(int str1Id、int str2Id、DateTime startDate、DateTime endDate)
{
var str=\u ManufacturerWidgetsRespository.GetManufacturerWidgetByManufacturerAndCountry(manufacturerId,countryId);
var报告=新ABCReport();
var reportList=new List();//没有关系的匿名类型的列表
report.reportList=新列表();
var计数=1;
foreach(str中的var mw)
{
//我在做一些函数
//为报告创建数据
}
返回局部视图(报告);
}
我试图在我的aspx中显示我的局部视图。
我的问题是,如果不点击我的提交按钮两次,我如何向我的控制器提交表单,并将我的部分视图插入我的aspx中。好
要实现这一目标,您需要做两件事:
PartialView
,该视图委托给您在InterStats
中调用的部分),而不是执行重定向操作
.submit()
操作需要重构,以便通过ajax
提交表单,然后在成功回调中接受返回的ActionResult
,并将其附加到页面上 [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);
}