2

I have a usercontrol named "LoginUserControl.ascx" which I have placed in a master page. Header of "LoginUserControl.ascx"

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MultiTechnologyWeb.Models.loginmodel>" %>

Then I used the below code to show the usercontrol in the masterpage.

<% Html.RenderPartial("LoginUserControl"); %>

On first run the page "index" is loaded. Notice the header of the "index" page, no model is specified. Thus page load successfully

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/MT.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

Now I click on the link to open register.aspx. I got the below error

The model item passed into the dictionary is of type 'MultiTechnologyWeb.Models.registermodel', but this dictionary requires a model item of type 'MultiTechnologyWeb.Models.loginmodel'.

Header of "register.aspx" page

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/MT.Master" Inherits="System.Web.Mvc.ViewPage<MultiTechnologyWeb.Models.registermodel>" %>

So to my understanding model is being interchanged, so anybody can please help me on how to resolve this issue

More Explanation.............LATEST

I have debug, i know that the crash is occuring after the actionresult for register is finished execution. Code below is for actionresult "register"

   public ActionResult register()
    {
        registermodel model;
        //some code here
    return View("register",model);
    }

So i'm just returning one type of model that is "registermodel", Would it be possible to return another model such as "loginmodel" by using a list or array to return multiple models in the same view.

Kevin
  • 73
  • 8

3 Answers3

0

You should use <% Html.RenderAction("Logon","Account"); %> in your MasterPage instead of using RenderPartial and in this action you just return the login partial you want to use in the header

public ActionResult Logon(){
// do your stuff
return PartialView("LoginUserControl");
}

By this way you could pass the loginmodel to the LogInPartial and pass registermodel to the register page

Wahid Bitar
  • 13,776
  • 13
  • 78
  • 106
  • I have changed to code to public ActionResult Logon() { loginmodel model =new loginmodel (); return PartialView("LoginUserControl", model); } – Kevin Mar 08 '12 at 12:11
  • Did u use `Html.RenderAction(...)` instead of `RenderPartial()` ?? – Wahid Bitar Mar 08 '12 at 12:51
0

Please not that RenderAction and RenderPartial are not the same.

RenderPartial will render only the view. While RenderAction will make a new MVC roundtrip, by making a new instance of the controller etc and returning the result.

To solve your issue you could pass in the MultiTechnologyWeb.Models.loginmodel where you call <% Html.RenderPartial("LoginUserControl"); %>. It would look like this:

<% Html.RenderPartial("LoginUserControl", new MultiTechnologyWeb.Models.loginmodel()); %>

Or:

<% Html.RenderPartial("LoginUserControl", Model.LoginModel); %>
J. Salman
  • 61
  • 3
  • I tried what you said but still doesn't work. I have just updated the issue description ....... check by line "More Explanation"............. – Kevin Mar 08 '12 at 16:08
0

If you're not wanting to send a model to your partial view, which I've wanted to do in the past, you do have to at least pass something to the RenderPartial method.

This was the only method I could find that allowed me to now have to pass a model. I tried passing null and it continued to pass the parent model

<% Html.RenderPartial("LoginUserControl", new ViewDataDictionary()); %>
Anthony Shaw
  • 8,146
  • 4
  • 44
  • 62