-2

Hi i am having following error while rendering the page.

The model item passed into the dictionary is of type 'XXX.Models.MWRCustomerModel', but this dictionary requires a model item of type 'XXX.Controllers.AdminCustSignUpModel'.

My C# code is

public ActionResult signUpTravelAdvantageVip(AdminCustSignUpModel AdminCustSignModel)
    {
        int number;
        string RefferedId = "";
        string sponsorUserName = AdminCustSignModel.SpoName;  

        var fullCountryName = db.MWRAutoStates.Where(x => x.Country == AdminCustSignModel.CountryShortCode).FirstOrDefault();
        string incomingCountry = fullCountryName.FullCountryName.ToString();


        ViewBag.Promoter = sponsorUserName;           
        var sponsor = db.GetUserInfo(sponsorUserName).SingleOrDefault(); 
        ViewBag.SponsorFirstName = sponsor.FName;
        ViewBag.SponsorLastName = sponsor.LName; 

        MWRCustomerModel model = new MWRCustomerModel();
        if (AdminCustSignModel.CountrySC == "US")
        {
            model.Sponsor = sponsorUserName;
        }
        else {
            model.Sponsor = sponsorUserName;
            model.EuroCustomer = true;
        }
        model.BillingCountry = AdminCustSignModel.CountrySC;

        return View(model);
    }

And my view is

@model WarrantyRewards.Models.MWRCustomerModel

    @{
       ViewBag.Title = "signUpTraAdv";
    }
   <h2>signUpTraAdv</h2>

I have searched and most of the answer related to "sending a list from back end but in the view using the model for a single object" or "sending one model and using different model at view page". But for me, i am sending single object using a model and using the same model at view. still getting the error.

Sam
  • 9
  • 9
  • Based on what you've shown, the only thing besides some form of corruption that I can think of that would cause that issue is that the view your code is actually using is not the one you have shown. – jmcilhinney Oct 08 '17 at 05:04
  • Maybe try searching for usages of `WarrantyRewards.Controllers.AdminCustSignUpModel` and placing breakpoints on them to see if they get hit. – jmcilhinney Oct 08 '17 at 05:05
  • Looking more closely, I see that your action has a parameter of type `AdminCustSignUpModel`. Where is that coming from? Is this action handling a post from a view with that model? If so then it seems likely that it would be returning that same view, hence the discrepancy. If you want to return a view that doesn't match the name of your action then you need to specify it's name when calling `View`. – jmcilhinney Oct 08 '17 at 05:08
  • Your controller method parameter is `AdminCustSignUpModel AdminCustSignModel` and your view model is `WarrantyRewards.Models.MWRCustomerModel`. They cannot be automatically cast to each other. – Amit Kumar Singh Oct 08 '17 at 05:34
  • What is the **exact** name of the file containing this line `@model WarrantyRewards.Models.MWRCustomerModel`? Does that file contain any reference to other (e.g. partial) views? – mjwills Oct 08 '17 at 05:36
  • @jmicilhinney , Thanks man. The actual picture is, i bind some data with AdminCcustSignUpModel in a action method called "AdminCustomerSignUp". i have submitted from the view and catch incoming data to its post method. then in this post method, based upon some condition, i have redirected the same model to a new action method called "public ActionResult signUpTravelAdvantageVip(AdminCustSignUpModel AdminCustSignModel)" now in this action method i called another model MWRCustomerModel and put data in the MWRCustomerModel's properties and return this model in the view. – Sam Oct 08 '17 at 05:44
  • @Amit my controller parameter cold be anything, it could even be a string or int but i have returned a specific model in the return statement. – Sam Oct 08 '17 at 05:52
  • You are most probably passing correct model to incorrect place. https://stackoverflow.com/questions/21483125/how-to-fix-the-model-item-passed-into-the-dictionary-is-of-type-error and https://stackoverflow.com/questions/40373595/the-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary-requ – Amit Kumar Singh Oct 08 '17 at 05:55
  • @mjwills the exact name of the file is the same as action name "signUpTravelAdvantageVip.cshtml" – Sam Oct 08 '17 at 08:11
  • @mjwills nope, no partial view. – Sam Oct 08 '17 at 08:29

1 Answers1

-2

After lots of wiring and tearing i have found the issue. I am explaining with example.

I got an action Method called testActionMethode_1 which view file is "testActionMethode_1.cshtml" and it is bind with "test_1" model.

when i submit the values form testActionMethode_1.cshtml view, the model received by [HTTPPost] "testActionMethode_1" action method.

Now in this Post action method i am doing something and based upon the value i am redirecting to another Actin method called testActionMethode_5 (still sending the same model test_1).

in the new Actin method testActionMethode_5, i am using values coming with the test_1 model properties.

The problem occurred at this point where i use another model "test_2" and send this model to view "testActionMethode_5.cshtml". But the post has not been finished yet which we initiated by pressing the submit button in the view file "testActionMethode_1.cshtml"

So while we were creating the view file "testActionMethode_5.cshtml" by binding model "test_2", the system was showing error and expecting model "test_1"

Sam
  • 9
  • 9
  • The use of made up action names (_1 etc) rather than method names mentioned in your question makes it very hard to relate the two of them. – mjwills Oct 08 '17 at 08:36