1

I am a beginner with ASP.NET MVC and I have a rather weird problem which I can't seem to work out how to fix. I have found multiple solutions to various versions of the same problem via google, but they don't seem to relate to my problem. If anyone would be kind enough to help me spot the mistake I would be grateful.

Edit: I know that changing @model app.Models.Authentication to @model app.Models.ContactMessage will fix it, but I don't understand why and obviously the propsed fix will give me a bunch of other errors related to the form.

My Error: The model item passed into the dictionary is of type 'app.Models.Auth', but this dictionary requires a model item of type 'app.Models.ContactMessage'

Relevant Q&A: Similar Problem #1 Similar Problem #2

Stack Trace:

[InvalidOperationException: The model item passed into the dictionary is of type 'app.Models.Authentication', but this dictionary requires a model item of type 'app.Models.ContactMessage'.]
System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value) +378
System.Web.Mvc.ViewDataDictionary.set_Model(Object value) +47
System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary) +259
System.Web.Mvc.ViewDataDictionary`1..ctor(ViewDataDictionary viewDataDictionary) +37
System.Web.Mvc.WebViewPage`1.SetViewData(ViewDataDictionary viewData) +98
System.Web.Mvc.WebViewPage.set_ViewData(ViewDataDictionary value) +39
System.Web.Mvc.WebViewPage.ConfigurePage(WebPageBase parentPage) +267
System.Web.WebPages.<>c__DisplayClass3.<RenderPageCore>b__2(TextWriter writer) +318
System.Web.WebPages.HelperResult.WriteTo(TextWriter writer) +42
System.Web.WebPages.WebPageExecutingBase.WriteTo(TextWriter writer, HelperResult content) +45
System.Web.WebPages.WebPageBase.Write(HelperResult result) +53
System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body) +178
System.Web.WebPages.WebPageBase.PopContext() +229
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +154
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +695
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +382
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +431
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +116
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +529
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +106
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +321
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +185
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
  System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +39
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9651188
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

Home Controller:

 public class HomeController : BaseController
{



    [HttpGet]
    public ActionResult Index()
    {

        ViewBag.Title="Portfolio";
        return View(new ContactMessage());
    }

    [HttpPost]
    public ActionResult Index(ContactMessage message)
    {
        if (ModelState.IsValid)
        {
            using (var db = new MessageDatabase())
            {

                message.DateSent = DateTime.Now;

                db.ContactMessages.Add(message);
                Success(string.Format("Message received! Thanks, I will try to get back to you as soon as possible."), true);
                db.SaveChanges();
            }

            TempData["ContactMessage"] = message;
            return RedirectToAction("Index");
        }
        Danger("Looks like something went wrong. Please check your form.");
        ViewBag.Title = "Portfolio";
        return View(message);
    }

   ...
}

Account Controller:

public class AccountController : BaseController
{
//
    // GET: /Account/

    public ActionResult Index()
    {
        return View();
    }

    [HttpGet]
    public ActionResult Login()
    {
        var authModel = new Authentication();
        return View(authModel);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Login(Authentication authModel)
    {
        if (!ModelState.IsValid)
        {
            return View(authModel);
        }

        if (WebSecurity.Login(authModel.Username, authModel.Password))
        {
            return new RedirectResult("/");
        }
        else
        {
            ViewBag.ErrorMessage = "The username and/or password was incorrect";
            return View(authModel);
        }

    }


    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Logout(Authentication authModel)
    {


        if (WebSecurity.Login(authModel.Username, authModel.Password))
        {
            WebSecurity.Logout();
            Success("You have been logged off", true);
            return new RedirectResult("/");
        }

    }

    [HttpGet]
    public ActionResult Register()
    {
        return View(new Registration());
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Register(Registration authModel)
    {
        if (ModelState.IsValid)
        {
            if (!WebSecurity.UserExists(authModel.Username))
            {
                WebSecurity.CreateUserAndAccount(authModel.Username, authModel.Password);
                WebSecurity.Login(authModel.Username, authModel.Password, true);
                return new RedirectResult("/");
            }

            ViewBag.ErrorMessage = string.Format("The user {0} already exists. Please try a different username.",
                authModel.Username);
        }


        return View(authModel);
    }
}

View that causes problems:

@model app.Models.Authentication


@{
ViewBag.Title = "Login";
}
<div class="container">
   <h2>Login</h2>

   <div class="form-horizontal">
       @if (WebSecurity.IsAuthenticated)
       {
          <p>You are currently logged in.</p>
       }
       else
       {
           <hr />
           <p>@ViewBag.ErrorMessage</p>
           using (Html.BeginForm())
           {
             @Html.AntiForgeryToken()
            <div class="form-group 12u">
                <p>
                    @Html.LabelFor(m => m.Username) @Html.ValidationMessageFor(m => m.Username)<br />
                    @Html.EditorFor(m => m.Username)
                </p>
            </div>
            <div class="form-group 12u">
                <p>
                    @Html.LabelFor(m => m.Password) @Html.ValidationMessageFor(m => m.Password)<br />
                    @Html.PasswordFor(m => m.Password)
                </p>
            </div>
            <div class="form-group 12u">
                <p>
                    <input type="submit" value="Login" />
                </p>
            </div>
            <div class="form-group 12u">
                <p>Don't have an account? @Html.ActionLink("Register", "Register")</p>
                <p>@Html.RouteLink("Return Home", new { controller = "Home" })</p>
            </div>
        }
    }
</div>

RoutesConfig file:

 public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
Community
  • 1
  • 1
f78xd23
  • 117
  • 2
  • 15
  • Well which controller is that a view for? If it's the home one, then look at what you're creating the view with - a `ContactMessage`... – Jon Skeet Sep 25 '15 at 16:08
  • The view code above is for AccountController, when i try to go website.net/account/login or website.net/register its showing me that error. – f78xd23 Sep 25 '15 at 16:12
  • 1
    can you post your routes? this looks like you aren't passing the right view for this action. – Claies Sep 25 '15 at 16:13
  • Have you set breakpoints and debugged into this to check which controller is actually being used? – Jon Skeet Sep 25 '15 at 16:18
  • I assume you meant RouteConfig file. if that's the case then I've done so (see end of post). – f78xd23 Sep 25 '15 at 16:19
  • I've set break points in the Login view and it went through the whole view and showed me an error after going through the last closing bracket. – f78xd23 Sep 25 '15 at 16:19

2 Answers2

8

My best guess is that somewhere, probably in your layout, you're doing something like:

 @Html.Partial("_Contact")

Intending to bring a contact form or something into maybe a sidebar area. When you call a partial, you implicitly pass the model of the current view, the same as if you were to do:

@Html.Partial("_Contact", Model)

If the partial doesn't utilize a model, then this won't necessarily cause problems. However, if your partial does define a model, then it must be passed an instance of that type. You could probably fix it by just doing something like:

@Html.Partial("_Contact", new ContactMessage())

However, with items like this in your layout, you should actually use child actions:

@Html.Action("Contact", "Home")

Then, you can define a Contact action in HomeController that returns your partial view and defines a model to be passed with it it. This takes the rendering of this block completely out of the context of the rest of the page, so you don't get conflicts from context bleed-over like you have here.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
1

I think that you want return RedirectToAction("ActionName", "ControllerName") instead of return RedirectResult("Path").

RedirectToAction - https://msdn.microsoft.com/en-us/library/system.web.mvc.controller.redirecttoaction(v=vs.118).aspx#M:System.Web.Mvc.Controller.RedirectToAction%28System.String,System.String%29

RedirectResult - https://msdn.microsoft.com/en-us/library/system.web.mvc.redirectresult(v=vs.118).aspx

Gary.S
  • 7,076
  • 1
  • 26
  • 36