asp.net-mvc - 在 MVC 应用程序中提交后如何返回相同的部分?
问题描述
我想在 MVC 应用程序中提交后返回相同的部分。我尝试了以下代码但无法正常工作。此外,如果我以这种方式传递部分,那么它工作正常if (Id == "A1"){
。但我不想这样做。
错误
传入字典的模型项的类型为“Ap.Models.Report.Report”,但该字典需要类型为“System.Collections.Generic.IEnumerable1['Ap.Models.Report.Report']”的模型项。 `
模型
public class Report
{
public string Currency { get; set; }
public IEnumerable<SelectListItem> CurrencyOptions { get; set; }
public string Distance { get; set; }
public IEnumerable<SelectListItem> DistanceOptions { get; set; }
}
控制器
[HttpPost]
public PartialViewResult Report(Report reports)
{
string Id = Session["ID"].ToString();
Data.DataSQL(reports);
ViewModel(reports); //gives me dropdownlist
if (Id == "A1")
{
return PartialView("~/Views/_PartialA", reports);
}
else if (Id == "A2")
{
return PartialView("~/Views/_PartialB", reports);
}
return PartialView(reports);
}
看法
@model IEnumerable<Ap.Models.Report.Report>
.....
@using (Html.BeginForm("Report", "Report", FormMethod.Post, new { id = "my" }))
{
<div id="mpartial"> </div>
<button type="submit" id="submit" class="btn btn-primary">Run</button>
</div>
}
_部分A
@model Ap.Models.Report.Report
@Html.DropDownListFor(m => m.Currency, Model.CurrencyOptions, new { @class = "form-control"})
@Html.DropDownListFor(m => m.Distance, Model.DistanceOptions, new { @class = "form-control"})
脚本
$(document).ready(function () {
var url = '@Url.Action("Report")';
$('#my').submit(function () {
if (!$(this).valid()) {
return;
}
$.post(url,$(this).serialize(), function (response) {
$('#mpartial').html(response);
});
return false;
})
});
解决方案
推荐阅读
- elasticsearch - Elasticsearch搜索嵌套字段的第一个元素
- robotframework - 通过 selenium2library 在 Firefox 浏览器中出现“已建立的连接被主机中的软件中止”错误
- javascript - 提交联系表格后如何到达页面上的特定位置?
- laravel - 如何使用 jwt 在 Laravel 中授权用户和管理员?
- c# - EF Core - 使用参考导航属性(外键)保存的模式
- solr - Solr:使用 * 时没有返回结果
- amazon-web-services - 如何在 CloudFormation 的安全组中同时指定 IPv6 和 v4?
- mysql - 如何正确编写 SQL 查询?
- c - 用数组中的数据填充结构
- php - 访问回调函数外的变量递归数组遍历