2 Stimmen

Konflikt mit Modellen bei partieller Ansicht und LogOn

Das an das Wörterbuch übergebene Modellelement ist vom Typ "System.Collections.Generic.List`1[PM.Models.Product]", aber dieses Wörterbuch erfordert ein Modellelement vom Typ "PM.Models.LogOnModel".

PROBLEM:

    '/'.
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[PM.Models.Product]', but this dictionary requires a model item of type 'PM.Models.LogOnModel'. 
 :      -.                . 

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

 : 

 1:  @using PM.Models
 2:  @{PM.Models.LogOnModel LOM=new LogOnModel();}
 3:  @RenderPage("../Account/LogOn.cshtml");

Ich versuche, eine PartialView auf der Hauptseite zu verwenden, um für Benutzer-Feld für Login und Passwort zu sehen, um sich auf Site.And andere partielle Ansicht für Benutzer Liste der Produkte auf Website präsentieren. Aber ich habe Problem, bitte helfen Sie mir mit ihr.

Dies ist ein Beispiel für meine Anmeldeseite

@model PM.Models.LogOnModel
@{
    ViewBag.Title = "Log On";
}
<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>
<script type="text/javascript" language="javascript">
    var UserNameTextBox = document.getElementById("login");
    UserNameTextBox.textContent = UserNameTextBox.textContent + "123";
</script>

<link href="../../Content/themes/our/Style.css" rel="stylesheet" type="text/css" />

@using (Html.BeginForm("LogOn", "Account"))
{      
   <div id="login" >
    @Html.TextBoxFor(m => m.UserName, new { @class = "inputLogin" })
    @Html.PasswordFor(m => m.Password, new { @class = "inputLogin" })
    @Html.CheckBoxFor(m => m.RememberMe)
    @Html.LabelFor(m => m.RememberMe, new { @class = "rememberText" })
    <div id="ErrorMessage">
    @Html.ValidationSummary(true, "  ,   .")
    </div>

     <div id="loginButtons">
            @Html.ActionLink(" ", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink", @class = "register" })
            <input type="submit" value="" id="loginButton" title="" />
        </div>
    <div id="loginWith">
         : &nbsp;&nbsp;&nbsp;
        <a href="" style="text-decoration:none;"><img alt="" class="SocialIcon" src="../../Content/img/VKicon.PNG" />&nbsp;&nbsp;&nbsp;&nbsp;</a>
        <a href="" style="text-decoration:none"><img alt="" class="SocialIcon" src="../../Content/img/FBIcon.PNG" />&nbsp;&nbsp;&nbsp;</a>
        <a href="" style="text-decoration:none"><img alt="" class="SocialIcon" src="../../Content/img/TwitterIcon.PNG" /></a>
    </div>
   </div>

}

Dies ist ein Beispiel für meine Suchseite, die ein anderes Modell als LogOn benötigt

@model IEnumerable<PM.Models.Product>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Description)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Type)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Image)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Partition)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Description)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Type)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Image)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Partition)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
}

</table>

Kontokontrolleur

 [AllowAnonymous]
        public ActionResult LogOn()
        {
            string actionName = ControllerContext.RouteData.GetRequiredString("action");
            ViewBag.FormAction = actionName;
            return View();
        }

        //
        // POST: /Account/LogOn

        [AllowAnonymous]
        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (System.Web.Security.Membership.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                    if (Url.IsLocalUrl(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        if (Request.UrlReferrer != null)
                            return Redirect(Request.UrlReferrer.AbsoluteUri);
                        else
                        {
                            return RedirectToAction("Index","Home");
                        }
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }

            // If we got this far, something failed, redisplay form
            return RedirectToAction("Index", "Home", new { login = "incorrect" });
        }

Controller, die mit Search View arbeiten

[HttpGet]
    public ActionResult Search(string KeyWord)
    {
        DataManager dm = new DataManager();
        List<Product> sended = dm.FindProducts(KeyWord);
        return View(sended);
    }

3voto

Darin Dimitrov Punkte 990883

Sie haben also 2 Teilansichten LogOn.cshtml und Search.cshtml, die jeweils stark auf LogOnModel y IEnumerable<Product> . Das bedeutet, dass Sie die richtigen Modelltypen übergeben müssen, wenn Sie diese Teilbilder rendern. Wenn Sie zum Beispiel nur die Html.Partial("somePartial") ohne Angabe eines Modells als zweites Argument, wird das übergeordnete Modell übergeben. Wenn Sie ein Modell angeben wollten, könnten Sie wie folgt vorgehen:

@{
    var logonModel = new LogOnModel();
}
@Html.Partial("~/Views/Account/LogOn.cshtml", logonModel)

oder Sie können die Html.Action Helper, der es Ihnen ermöglicht, anstatt eine partielle Ansicht direkt einzubinden, eine Controller-Aktion aufzurufen, die eine partielle Ansicht zurückgibt. Es wird jedoch keine separate HTTP-Anfrage vom Client gesendet. Alles geschieht in der gleichen Anfrage:

@Html.Action("LogOn", "Account")

und jetzt wird Ihre LogOn-Methode aufgerufen und muss das richtige Modell übergeben:

public ActionResult LogOn()
{
    var model = new LogOnModel();
    return PartialView(model);
}

[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
    ...
}

Das Gleiche gilt für die Teilsuche:

@Html.Action("Search", "SomeControllerContainingTheSearchAction")

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X