我有一个正在接收类型模型的操作:
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# 中,您可以传递一个接口,然后根据需要在适当的时候将其转换为您“知道”所需的类型。我怀疑这与自动模型绑定有关,但不确定,并且想知道如何解决它,否则我怀疑我正在 for ([n] * search models) 复制/粘贴代码更改仅为模型类型.
希望这是有道理的。