2012-04-13 12 views
2

辞書に渡されるモデルアイテムの型は '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"); 

私はSite.Andでのユーザリストに存在のための別の部分図をサインインするためにログインとパスワードをユーザフィールドに表示するメインページで使用する1つPartialViewを使用するようにしてくださいサイトの製品。 しかし、私は問題がある、彼女と私を助けてください。

これは、このログオン

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

アカウントConroller

[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" }); 
     } 

コントローラー作業別のモデルを必要と私の検索ページの一例である私のログオンページの例

@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); 
    } 
+0

対応するコントローラーのアクションを表示できますか? –

+0

いや、数分前に追加します。 –

+0

あなたの例では、1つのコントローラが強く型付けされたビューを作成したものよりも1つのコントローラを持っていると認識されない強く型付けされたビューに別のモデルを渡すことになるので、このエラーが発生します –

答えて

2

したがって、LogOn.cshtmlとSearch.cshtmlは、それぞれLogOnModelIEnumerable<Product>に厳密にタイプされています。これは、これらの部分をレンダリングするときに、正しいモデルタイプを渡す必要があることを意味します。たとえば、モデルを第2引数として指定せずにHtml.Partial("somePartial")しか使用しない場合は、親モデルが渡されます。あなたは次のことを行うことができモデルを指定したい場合は、次の

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

またはあなたの代わりに、直接部分図を含めてのHtml.Actionヘルパーを使用することができますが、部分的に返されますコントローラのアクションを呼び出すことができます。しかし、クライアントから別々の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") 
+0

本当にありがとう=))))) –

関連する問題