我有一个接收类型的模型的动作:
ExchangeBooksSearchViewModel : ExchangeBasicSearchViewModel
所以当我回帖时,我在该模型中有关于作者等的信息。所以我将其传递给视图:
public ActionResult Books(ExchangeBooksSearchViewModel searchModel, int? page)
{
..
return View("Index", searchModel);
}
...
@model Ui.Mvc.Models.ExchangeBasicSearchViewModel
@{
ViewBag.Title = metaExchange.Index_PageTitle;
ViewBag.ActionName = "Index";
}
@section InlineTitle {<h2>@metaExchange.Index_InlineTitle</h2>
}
@section SearchArea { @Html.Partial("_ItemsSearch_ManagerPartial", Model)}
@Html.Partial("_ItemsList_AjaxPartial", Model)
@section HiddenPostFields { }
@section scripts{}
然后在_ItemsSearch_ManagerPartial中我调用一个动作来显示模型的相应搜索Ui:
<div id="AdvancedCategorySearch">
@Html.Action("NonAjaxPostbackCategorySearchDisplay", Model)
</div>
...
[AllowAnonymous]
public ActionResult NonAjaxPostbackCategorySearchDisplay(ExchangeBasicSearchViewModel searchModel)
{
if (searchModel.CategoryAction == "Books")
{
return PartialView("_ItemsSearch_Books", new ExchangeBooksSearchViewModel());
}
if (searchModel.CategoryAction == "Computers")
{
return PartialView("_ItemsSearch_Computers", new ExchangeComputersSearchViewModel());
}
return PartialView("_ItemsSearch_Basic", new ExchangeBasicSearchViewModel());
}
问题在于我到达时
NonAjaxPostbackCategorySearchDisplay(ExchangeBasicSearchViewModel searchModel)
我只有基本类型的ExchangeBasicSearchViewModel才能使用,即使我尝试强制转换::
ExchangeBasicSearchViewModel as ExchangeBooksSearchViewModel
我得到了空。这个我不明白。我意识到我的视图的模型类型为
@model Ui.Mvc.Models.ExchangeBasicSearchViewModel
其中的一点是允许子类型传入视图的泛型函数,但由于这一切仍在服务器上发生,我不明白我的子类型被拆分为基类型的位置?
在“普通”c#中,您可以传递一个界面,然后根据需要投射到您在适当的位置“知道”的类型。我怀疑这与自动模型绑定有关但不确定,并且想知道如何解决它,否则我怀疑我是([n] *搜索模型)复制/粘贴代码只更改模型类型。
希望这是有道理的。
答案 0 :(得分:0)
视图上的视图模型不正确。 你应该使用:
@model Ui.Mvc.Models.ExchangeBookSearchViewModel
ExchangeBasicSearchViewModel应该只是一个抽象类