1
votes

I'm new to exploring MVC3 and came up with a problem.

I'm editing my Master view and want the login boxes to be visible on every page when a user isn't logged in. I've done this by using this code:

@if (Request.IsAuthenticated)
        {
<text>Welcome <strong>@User.Identity.Name</strong>!
[ @Html.ActionLink("Log Off", "LogOff", "Account") ]</text>
        }
        else
        {
           Html.RenderPartial("../Account/LogOn");
        }

This works when going to my normal Index method of the HomeController.

However, when going to the Index method of my NewsController I get the following error:

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[LeagueSite.Models.News]', but this dictionary requires a model item of type 'LeagueSite.Models.LogOnModel'.

I understand what the problem is but don't really know a sollution for it.

The view LogOn looks like this (standard MVC3 logon view):

@model LeagueSite.Models.LogOnModel

@{
    ViewBag.Title = "Log On";
}

<h2>Login</h2>
<p>
    Please enter your user name and password. @Html.ActionLink("Register", "Register") if you don't have an account.
</p>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.")

@using (Html.BeginForm()) {
    <div>
        <fieldset>
            <legend>Account Information</legend>

            <div class="editor-label">
                @Html.LabelFor(m => m.UserName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.UserName)
                @Html.ValidationMessageFor(m => m.UserName)
            </div>

            <div class="editor-label">
                @Html.LabelFor(m => m.Password)
            </div>
            <div class="editor-field">
                @Html.PasswordFor(m => m.Password)
                @Html.ValidationMessageFor(m => m.Password)
            </div>

            <div class="editor-label">
                @Html.CheckBoxFor(m => m.RememberMe)
                @Html.LabelFor(m => m.RememberMe)
            </div>

            <p>
                <input type="submit" value="Log On" />
            </p>
        </fieldset>
</div>

} `

Any tips/ideas?

2

2 Answers

1
votes

Quick and dirty: instead of Html.RenderPartial use Html.RenderAction:

Html.RenderAction("LogOn", "Account");

and in the LogOn action of the Account controller ensure to return a PartialView or you will get a stack overflow and Cassini will crash:

public ActionResult LogOn()
{
    return PartialView();
}
1
votes

I think your "news" view has already a model associated to it. maybe it starts like this?:

@model LeagueSite.Models.News

well, if so, if you are not passing a model to your partial view, then the framework assumes by default that the model for that partial is "LeagueSite.Models.News", which of course isn't what you want. You must pass the LogOnModel to your LogOn partial view like this:

 Html.RenderPartial("../Account/LogOn", Model.ObjLogonModel);

this assumes that you have an instance of LogonModel into your "News" model. then you'll be able to handle the Logon action regards