c# - MVC 3 多表单模型传递给字典

标签 c# asp.net-mvc-3 view model partial-views

我是 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 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>

第二个在搜索框中:

@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() 发布某种形式时,例如。注册,我收到以下错误:

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

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

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

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

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

有没有人知道如何解决这个问题?非常感谢。

最佳答案

@{Html.RenderPartial("MenuTop", new Models.LogOnModel());}
@{Html.RenderPartial("SearchBox", Enumerable.Empty<Product>());}

关于c# - MVC 3 多表单模型传递给字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9274826/

相关文章:

jquery - 如何根据服务器端决策有条件地运行 jQuery

ios - 随机切换 View 的代码不起作用

c# - WPF OpenFileDialog 与 MVVM 模式?

c# - 在代码优先环境中从包管理器控制台更新数据库

c# - C#异步而不创建新线程

c# - ASP.NET MVC3和SQL DB:如何编写每天应调用一次的过程?

c# - 错误与 if else 和 View 中的标记

asp.net-mvc - 如何从 MVC3 中的区域 Controller 中出来?

java - convertView.getTag();创建转换错误

javascript - 对于 Opera Mini 使用 javascript 检测移动 View 是否打开或关闭