MVC 3 多种形式模型传递给字典

发布于 2025-01-05 11:48:52 字数 2616 浏览 1 评论 0原文

我对 MVC 3 很陌生,并且遇到了一个问题。主要布局如下所示:

<body>
<div id="conteiner_body">
    <div id="conteiner_main">
        <div id="container_top">
            @{ Html.RenderPartial("Basket"); }
            @{ Html.RenderPartial("MenuTop"); }
        </div>
        <div id="left_side_border">
            <div id="left_side">
                @{ Html.RenderPartial("SearchBox"); }
                @{ Html.RenderAction("Index", "LeftMenu"); }
            </div>
        </div>
        <div id="content">
            @RenderBody()
        </div>
        @{ Html.RenderPartial("Footer"); }
    </div>
</div>
</body>

页面上有 3 个表单。第一个是在 MenuTop 视图中:

@model Models.LogOnModel
<div id="conteiner_menu_top">
    <div id="menu_top">
        <div id="login">
            <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>

            @{
                using (Html.BeginForm("LogOn", "Account"))
                {
                    @Html.TextBoxFor(m => m.UserName)
                    @Html.PasswordFor(m => m.Password)
                    @Html.CheckBoxFor(m => m.RememberMe)
                    <input type="submit" value="Login" />
                    <input type="button" value="Registr" onclick="document.location.href = '/Account/Register'" />
                }
            }
        </div>
    </div>
</div>

第二个是在 SearchBox 中:

@using (Html.BeginForm("Search", "SearchBox"))
{
<div id="search_box">
    <h2>Search</h2>
        <input name="searchWord" class="text" type="text" />
        <a href="rozsirene_vyhledavani">Advanced</a>
        <input class="submit" type="submit" value="Search" />
</div>

当然还有 RenderBody 中的表单,它根据上下文而变化。 问题是,当我从 RenderBody() 发布某种表单时,例如。注册时,我收到以下错误:

传入字典的模型项是类型 'Models.RegisterModel',但是这个字典需要一个模型项 输入“Models.LogOnModel”。

我尝试向 MenuTop 和 SearchBox 添加它们自己的字典:

Html.RenderPartial("MenuTop", new ViewDataDictionary(Models.LogOnModel))
Html.RenderPartial("SearchBox", new ViewDataDictionary(IEnumerable<Product>))

但在这种情况下,我收到以下错误:

CS0119:“Models.LogOnModel”是一种“类型”,在给定上下文中无效

有人知道如何解决这个问题吗?多谢。

I'm quite new to MVC 3 and I'm stuck on an issue. The main layout look like this:

<body>
<div id="conteiner_body">
    <div id="conteiner_main">
        <div id="container_top">
            @{ Html.RenderPartial("Basket"); }
            @{ Html.RenderPartial("MenuTop"); }
        </div>
        <div id="left_side_border">
            <div id="left_side">
                @{ Html.RenderPartial("SearchBox"); }
                @{ Html.RenderAction("Index", "LeftMenu"); }
            </div>
        </div>
        <div id="content">
            @RenderBody()
        </div>
        @{ Html.RenderPartial("Footer"); }
    </div>
</div>
</body>

I have 3 forms on the page. First is in MenuTop view:

@model Models.LogOnModel
<div id="conteiner_menu_top">
    <div id="menu_top">
        <div id="login">
            <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>

            @{
                using (Html.BeginForm("LogOn", "Account"))
                {
                    @Html.TextBoxFor(m => m.UserName)
                    @Html.PasswordFor(m => m.Password)
                    @Html.CheckBoxFor(m => m.RememberMe)
                    <input type="submit" value="Login" />
                    <input type="button" value="Registr" onclick="document.location.href = '/Account/Register'" />
                }
            }
        </div>
    </div>
</div>

The second is in SearchBox:

@using (Html.BeginForm("Search", "SearchBox"))
{
<div id="search_box">
    <h2>Search</h2>
        <input name="searchWord" class="text" type="text" />
        <a href="rozsirene_vyhledavani">Advanced</a>
        <input class="submit" type="submit" value="Search" />
</div>

and of course the form in RenderBody, which changes according to the context.
Problem is, when I post some form from RenderBody(), eg. Registration, I recieve following error:

The model item passed into the dictionary is of type
'Models.RegisterModel', but this dictionary requires a model item of
type 'Models.LogOnModel'.

I've tried to add to MenuTop and SearchBox their own dictionary:

Html.RenderPartial("MenuTop", new ViewDataDictionary(Models.LogOnModel))
Html.RenderPartial("SearchBox", new ViewDataDictionary(IEnumerable<Product>))

But in this case I get following error:

CS0119: 'Models.LogOnModel' is a 'type', which is not valid in the given context

Has anybody any idea, how to solve this? Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

负佳期 2025-01-12 11:48:52
@{Html.RenderPartial("MenuTop", new Models.LogOnModel());}
@{Html.RenderPartial("SearchBox", Enumerable.Empty<Product>());}
@{Html.RenderPartial("MenuTop", new Models.LogOnModel());}
@{Html.RenderPartial("SearchBox", Enumerable.Empty<Product>());}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文