为什么当我通过“”时会填充此视图模型进入渲染动作?

发布于 2024-09-16 18:43:20 字数 1993 浏览 3 评论 0原文

我正在尝试实现一个存在于系统中每个页面上的小部件控件,这将允许用户进行基本的搜索和搜索。每个页面上都提供目录功能。这是下面定义的选项卡控件,其中

    中当前选定的选项卡由 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>

我想要加载 SearchDirectory 的原因是页面不必根据单击的选项卡来请求内容。我希望这一切都能立即可用。

我遇到的问题是,如果 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
    }

两者都 可以吗? DirectoryViewModelSearchViewModel 包含一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半透明的墙 2024-09-23 18:43:20

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.

草莓味的萝莉 2024-09-23 18:43:20

你应该这样做吗?

 <% Html.RenderAction("SearchBox", "Search", 
                    new { vm = ((Model.CurrentTab == "Search") ? Model.Search : "") }); %>

因为 Html.RenderAction 的第三个参数是 object routeValues ,它是一个字典,其中包含您作为 调用的 Action 的参数>密钥。如果您没有指定在 Html.RenderActionrouteValues 参数中传递的参数,它将始终将 object 值传递给Action 的 vm 参数。

Should you be doing something like this

 <% Html.RenderAction("SearchBox", "Search", 
                    new { vm = ((Model.CurrentTab == "Search") ? Model.Search : "") }); %>

Since the third parameter of the Html.RenderAction is object routeValues which is a dictionary with a parameter of the Action you are calling as a Key. If you don't specify the parameter that you are passing in your routeValues parameter of your Html.RenderAction it will always pass an object value to the vm parameter of your Action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文