在MVC应用程序中提交后如何返回相同的部分?

时间:2018-05-16 10:38:17

标签: asp.net-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>

            }

_PartialA

@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;
               })
        });

0 个答案:

没有答案