Asp.net 部分查看和登录时与模型冲突

Asp.net 部分查看和登录时与模型冲突,asp.net,asp.net-mvc,razor,Asp.net,Asp.net Mvc,Razor,传递到字典的模型项的类型为“System.Collections.Generic.List`1[PM.Models.Product]”,但此字典需要类型为“PM.Models.LogOnModel”的模型项 问题: Ошибка сервера в приложении '/'. The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[PM.Models.Product]',

传递到字典的模型项的类型为“System.Collections.Generic.List`1[PM.Models.Product]”,但此字典需要类型为“PM.Models.LogOnModel”的模型项

问题:

Ошибка сервера в приложении '/'.
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");
我尝试在主页上使用一个PartialView来查看登录用户字段和登录站点的密码。另一个PartialView用于显示站点上的产品列表。 但我有问题,请帮我照顾她

这是我的登录页面的示例

@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>

}
使用搜索视图的控制器

[HttpGet]
    public ActionResult Search(string KeyWord)
    {
        DataManager dm = new DataManager();
        List<Product> sended = dm.FindProducts(KeyWord);
        return View(sended);
    }
[HttpGet]
公共操作结果搜索(字符串关键字)
{
DataManager dm=新的DataManager();
List sended=dm.FindProducts(关键字);
返回视图(已发送);
}

因此,您有两个部分视图LogOn.cshtml和Search.cshtml,分别强键入
LogOnModel
IEnumerable
。这意味着您需要在呈现这些部分时传递正确的模型类型。例如,如果只使用
Html.Partial(“somePartial”)
而不指定模型作为第二个参数,则将传递父模型。如果要指定模型,可以执行以下操作:

@{
    var logonModel = new LogOnModel();
}
@Html.Partial("~/Views/Account/LogOn.cshtml", logonModel)
或者您可以使用帮助器,它不直接包括局部视图,而是允许您调用将返回局部视图的控制器操作。但是没有从客户端发送单独的HTTP请求。所有这些都发生在同一个请求中:

@Html.Action("LogOn", "Account")
现在您的登录方法将被调用,并且必须通过正确的模型:

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

[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
    ...
}
这同样适用于部分搜索:

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

你能显示相应的控制器动作吗?是的,我几分钟前刚刚添加了它们。在你的示例中,你有一个控制器,而你有一个强类型视图,然后你尝试将另一个模型传递给一个无法识别的强类型视图,因此你会遇到这个错误是的,我创建了强类型视图,使用一个为用户提供登录表单,另一个为用户提供搜索结果。请说,我如何才能做到这一点,或者如果我使用强类型视图,我不能使用两个视图,我必须使用另一种方法将数据从控制器传输到视图,例如ViewData?
public ActionResult LogOn()
{
    var model = new LogOnModel();
    return PartialView(model);
}

[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
    ...
}
@Html.Action("Search", "SomeControllerContainingTheSearchAction")