0

I want to return the same partial after submit in MVC application. I tried following code but not working. Also if I pass the partial in this way then it works fine if (Id == "A1"){. But I do not want do in this way.

Error

The model item passed into the dictionary is of type 'Ap.Models.Report.Report', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1['Ap.Models.Report.Report']'.`

Model

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

}

Controller

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

View

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

Script

 $(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;
               })
        });
User9895
  • 313
  • 2
  • 5
  • 16
  • You have not shown us the code causing that issue - its due to a view that has `@model IEnumerable` –  May 16 '18 at 10:42
  • And what do you mean you do not want to use `if (Id == "A1")`? What do you want to do? –  May 16 '18 at 10:43
  • @StephenMuecke, In Parent view I am using `IEnumerable` but in the partial view. – User9895 May 16 '18 at 10:45
  • Do you mean the the `ABC.cshtml` view? –  May 16 '18 at 10:45
  • @StephenMuecke, As you suggested before, I do not have to recall the same partial twice so I do not want to use something like this `if (Id == "A1")` – User9895 May 16 '18 at 10:46
  • You have not answered my question, Your error is because you are returning a view that has `IEnumerable` which I a guessing is `ABC.cshtml`, which means that your `if` and `else if` blocks are not executed so the `return PartialView(reports);` is. –  May 16 '18 at 10:49
  • @StephenMuecke, Sorry My view is `Report.cshtml` which is `IEnumerable` – User9895 May 16 '18 at 10:51
  • @StephenMuecke, My `if` and `else if` executes currently but does not execute `return PartialView(reports);`. But I do not want to use `if` and `else if` block there. – User9895 May 16 '18 at 10:53
  • The that is what is causing the exception (and I will dupe this). If for some reason you need to return the `Report.cshtml` view, then you need to pass a collection of `Report` - for example you could do `var model = new List(); model.Add(reports); return View(model);` –  May 16 '18 at 10:54
  • @StephenMuecke, But i do not want to return the view. My current scenario where I have partial view and i enter some data there and then submit the partial. After submit, I want to be in the same partial. – User9895 May 16 '18 at 10:57
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/171141/discussion-between-stephen-muecke-and-user9895). –  May 16 '18 at 10:58

0 Answers0