为什么当我通过“”时会填充此视图模型进入渲染动作?
我正在尝试实现一个存在于系统中每个页面上的小部件控件,这将允许用户进行基本的搜索和搜索。每个页面上都提供目录功能。这是下面定义的选项卡控件,其中
中当前选定的选项卡由 Model.CurrentTab
中的值和我想要的相应内容确定显示(基本上,使可见)也由该值决定。<div class="WidgetControl">
<ul class="WidgetTab tabs">
<li <%= (Model.CurrentTab == "Search") ? "class='active'" : "" %>>
<span href='<%= Url.Action("SearchBox", "Search") %>'>Search</span>
</li>
<li <%= (Model.CurrentTab == "Directory") ? "class='active'" : "" %>>
<span href='<%= Url.Action("DirectoryList", "Group") %>'>Directory</span>
</li>
</ul>
<div id="Search" class="tab_container">
<% Html.RenderAction("SearchBox", "Search"
, (Model.CurrentTab == "Search") ? Model.Search : ""); %>
</div>
<div id="Directory" class="tab_container">
<% Html.RenderAction("DirectoryList", "Group"
, (Model.CurrentTab == "Directory") ? Model.Search : ""); %>
</div>
</div>
我想要加载 Search
和 Directory
的原因是页面不必根据单击的选项卡来请求内容。我希望这一切都能立即可用。
我遇到的问题是,如果 CurrentTab
包含值“Directory”,这意味着(我假设)Html.RenderAction("SearchBox"...
应该传入但是当它到达操作方法时,传递到 SearchBox
的视图模型包含一个值,而不是 ""
我不明白为什么会发生这种情况。即使我将空字符串传递到 SearchBox
中,视图模型仍然包含一个值。有人可以解释一下发生了什么吗?
更新:
public PartialViewResult DirectoryList(DirectoryViewModel vm)
{
return PartialView(vm.Search); // this is expecting a string
}
public PartialViewResult SearchBox(SearchViewModel vm)
{
return PartialView(vm); // the among other things, the Search string is used
}
两者都 可以吗? DirectoryViewModel
和 SearchViewModel
包含一个名为 Search
的属性
I'm trying to implement a Widget control that exists on every page in the system, which will allow the user to have basic Search & Directory functionality available on each page. This is a tab control defined below, where in the <ul>
the currently selected tab is determined by the the value in Model.CurrentTab
and the corresponding content that I want to display (basically, make visible) is also determined by that value.
<div class="WidgetControl">
<ul class="WidgetTab tabs">
<li <%= (Model.CurrentTab == "Search") ? "class='active'" : "" %>>
<span href='<%= Url.Action("SearchBox", "Search") %>'>Search</span>
</li>
<li <%= (Model.CurrentTab == "Directory") ? "class='active'" : "" %>>
<span href='<%= Url.Action("DirectoryList", "Group") %>'>Directory</span>
</li>
</ul>
<div id="Search" class="tab_container">
<% Html.RenderAction("SearchBox", "Search"
, (Model.CurrentTab == "Search") ? Model.Search : ""); %>
</div>
<div id="Directory" class="tab_container">
<% Html.RenderAction("DirectoryList", "Group"
, (Model.CurrentTab == "Directory") ? Model.Search : ""); %>
</div>
</div>
The reason I want to load both Search
and Directory
is so the page doesn't have to request the content depending on which tab is clicked. I want it all to be available immediately.
The problem I'm having is that if CurrentTab
contains the value "Directory" this means (I assumed) that Html.RenderAction("SearchBox"...
should pass in an empty string. But when it gets to the action method, the View Model passed into SearchBox
contains a value and not ""
I don't understand why this is happening. Even when I pass an empty string into SearchBox
, the View Model still contains a value. Can somebody please explain what's going on? Should I be doing this differently?
update:
public PartialViewResult DirectoryList(DirectoryViewModel vm)
{
return PartialView(vm.Search); // this is expecting a string
}
public PartialViewResult SearchBox(SearchViewModel vm)
{
return PartialView(vm); // the among other things, the Search string is used
}
Both DirectoryViewModel
and SearchViewModel
contain a property called Search
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ModelBinder 将 new() 更新 ActionMethod 参数中的任何对象。我认为如果不实现您自己的 modelbidner,就无法关闭此行为。您需要创建一个没有参数的重载并相应地路由它。
The ModelBinder will new() up any object in a ActionMethod's parameters. I don't think this behavior can be turned off without implementing your own modelbidner. You need to create an overload that has no parameters and route it accordingly.
你应该这样做吗?
因为
Html.RenderAction
的第三个参数是object routeValues
,它是一个字典,其中包含您作为调用的 Action 的参数>密钥
。如果您没有指定在Html.RenderAction
的routeValues
参数中传递的参数,它将始终将object
值传递给Action 的vm
参数。Should you be doing something like this
Since the third parameter of the
Html.RenderAction
isobject routeValues
which is a dictionary with a parameter of the Action you are calling as aKey
. If you don't specify the parameter that you are passing in yourrouteValues
parameter of yourHtml.RenderAction
it will always pass anobject
value to thevm
parameter of your Action.