首页 > 解决方案 > 在 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;
               })
        });

标签: asp.net-mvc

解决方案


推荐阅读