0

I want to return both PartialView and View in the same ActionResult. What I want is that if (model.spartial.Count is equal to 0 then return the main View ABC else return the _Partial.

Note: JQuery code is updated. Error:

System.InvalidOperationException: 'The model item passed into the dictionary is of type 'Ap.Models.ABC.ClsS', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[Ap.Models.ABC.SectionPartial]'

Please suggest me where I am going wrong.

Model

  public class ClsS
    {    
        public string MM { get; set; } //dropdown
        public string CC { get; set; } //dropdown

        public List<SectionPartial> sectionPartial { get; set; } 
    }

    public class SectionPartial
    {
        public string Name { get; set; }
    }

Controller

public ActionResult Partial(string cc, string mm)
    {
        Details details = new Details();
        var model = new ClsS();

        model.spartial = details.spartialS(cc, mm);
        if (model.spartial.Count > 0)
        {
            return PartialView("_Partial", model);
        }
        else
        {
            return PartialView("_Partial", model);
        }
    }

_Partial

@model Ap.Models.ABC.ClsS

 @{ 
  Html.RenderPartial("_Section", Model.sectionPartial);
 }

JQuery '#CC,#MM' is dropdownlist and '#Rial' is the <div> where I am displaying this partial.

$('#CC,#MM').change(function () {
  var url = '@Url.Action("partial", "ABC")'
  $('#Rial').load(url, { cc: $('#CC').val(),mm: $('#MM').val() })
});
User9895
  • 313
  • 2
  • 5
  • 16
  • There's not nearly enough information here to diagnose. `XRegExp` is not mentioned anywhere in your code or description. – Sam Axe May 15 '18 at 01:35
  • @SamAxe Sounds similar problem like this: https://stackoverflow.com/questions/50233609/how-to-load-2nd-partial-based-on-name-value-from-1st-partial-in-parent-view. Because the request come from jQuery AJAX call, `return View()` certainly can't be used with `@Html.RenderPartial()` helper (the action should just return partial view). – Tetsuya Yamamoto May 15 '18 at 01:38
  • @SamAxe, I updated the question. Please have a look. – User9895 May 15 '18 at 01:42
  • Returning a full view in your ajax call does not really make sense - you would not want to repeat the layout, css, scripts etc. Its not really clear what you want to achieve with this. Your `else` block should also be returning a partial view. –  May 15 '18 at 01:46
  • @StephenMuecke, I want to return the partial view. But if the count is `0` then want to return the partial view with no data. – User9895 May 15 '18 at 01:48
  • So why not `return PartialView("_Partial", new ClsS());`? (and I assume the error is related to that fact that your `ABC.cshtml` view is using [XRegExp](http://xregexp.com/) and therefore its loaded twice) –  May 15 '18 at 01:50
  • @StephenMuecke, If I return the way you have shown here, I am getting following error `System.InvalidOperationException: 'The model item passed into the dictionary is of type 'Ap.Models.ABC.ClsS', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Ap.Models.ABC.SectionPartial]'.'` – User9895 May 15 '18 at 02:04
  • @User9895 Then you should pass `SectionPartial` as model argument instead of `ClsS` in `return PartialView(...)`. Not sure how to convert into `SectionPartial` because no part in your code mentions that. – Tetsuya Yamamoto May 15 '18 at 02:06
  • To understand that error, read [this Q/A](https://stackoverflow.com/questions/40373595/the-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary-requ). You have not shown what the models are or what the views are so we cannot help –  May 15 '18 at 02:06
  • @StephenMuecke, I have updated question with modal and partial. Please have a look. – User9895 May 15 '18 at 02:25
  • @TetsuyaYamamoto, I have updated the question with modal and partial view. Please guide me. – User9895 May 15 '18 at 02:26
  • Then you would not be getting the error you claim if you have used `return PartialView("_Partial", new ClsS());` since the model is `@model ClsS` –  May 15 '18 at 02:28
  • @StephenMuecke, I did the same as you suggested but still the error persist. – User9895 May 15 '18 at 02:32
  • If you had read the Q/A I linked to you would know why :) - it is now because `sectionPartial` is `null` so `Html.RenderPartial("_Section", Model.sectionPartial);` is passing `ClsS` to your `_Section.cshtml` partial which expects a IEnumerable` model –  May 15 '18 at 02:36
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/171040/discussion-between-stephen-muecke-and-user9895). –  May 15 '18 at 02:49

0 Answers0