1

我正在使用 ASP.NET MVC 2 和 VS 2010。我有两个视图:
view1用于在数据库中创建记录。
view2用于列出数据库中的数据。

我正在尝试合并这些视图,以便我可以使用 4 个 .aspx 文件在一个 .aspx 文件中看到它们:
Views/Shared/view1.aspx
Views/Shared/view2.aspx
Views/Home/index.aspx(这里是部分视图的代码)
Views/Shared/ChartLayout.Master (这个根本不是必需的,它只是用于 CSS 测试)

错误:传入字典的模型项的类型为“System.Data.Objects.ObjectSet`1[BookingApp.Models.BookingTableSet]”,但此字典需要“BookingApp.Models.BookingTableSet”类型的模型项。

如果我将 view1 留空,一切正常。

这是我的代码,

索引.aspx

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content>

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
<h2><%= Html.Encode(ViewData["Message"]) %></h2>
<div style="float:left"><%Html.RenderPartial("view1"); %></div>
<div style="float:left"><%Html.RenderPartial("view2"); %></div>
<div style="clear:both">
</div>  
</asp:Content> 


视图1.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"      Inherits="System.Web.Mvc.ViewPage<BookingApp.Models.BookingTableSet>"  %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Create
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Create</h2>
<% using (Html.BeginForm()) {%>
    <%: Html.ValidationSummary(true) %>

    <fieldset>
        <legend>Fields</legend>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Id) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.Id) %>
            <%: Html.ValidationMessageFor(model => model.Id) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Name) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.Name) %>
            <%: Html.ValidationMessageFor(model => model.Name) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Quality) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.Quality) %>
            <%: Html.ValidationMessageFor(model => model.Quality) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.IsBooked) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.IsBooked) %>
            <%: Html.ValidationMessageFor(model => model.IsBooked) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.DataBooked) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.DataBooked) %>
            <%: Html.ValidationMessageFor(model => model.DataBooked) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.TimeBooked) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.TimeBooked) %>
            <%: Html.ValidationMessageFor(model => model.TimeBooked) %>
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
<% } %>
<div>
    <%: Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>


我敢打赌这与继承有关,但我不知道是什么。谢谢你的帮助。

编辑
Site.Master:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="page">
    <div id="header">
        <div id="title">
            <h1>My MVC Application</h1>
        </div>              
        <%-- %> <div id="logindisplay">
            <% Html.RenderPartial("LogOnUserControl"); %>
        </div> %>            
        <div id="menucontainer">          
           <%-- %> <ul id="menu">              
                <li><%: Html.ActionLink("Home", "Index", "Home")%></li> 
                <li><%: Html.ActionLink("About", "About", "Home")%></li> 
            </ul> --%>          
        </div>
    </div>
    <div id="main">
        <asp:ContentPlaceHolder ID="MainContent" runat="server" />
        <div id="footer">
        </div>
    </div>
</div>
</body>
</html>

控制器:

[HttpPost]
    public ActionResult Create([Bind(Exclude = "Id")] BookingTableSet movieToCreate)
    {
        if (!ModelState.IsValid)
            return View();

        _db.AddToBookingTableSets(movieToCreate);
        _db.SaveChanges();
        return RedirectToAction("Index");
    }

编辑2

基本上我所拥有的是这两个教程合并在一起: http ://www.asp.net/mvc/tutorials/older-versions/movie-database/create-a-movie-database-application-in-15-minutes-with -asp-net-mvc-cs

http://jeffreypalermo.com/blog/asp-net-mvc-and-the-templated-partial-view-death-to-ascx/

也许这将有助于理解我想要做什么。

4

2 回答 2

0

您需要像 Html.RenderPatial("view1", Model.BookingSet); 那样明确地传递模型

其中 BookingSet 是 view1 所需的模型

传递给控制器​​视图的视图数据字典只有一个模型,而 view1 的 aspx 需要一个不同类型的模型

于 2012-06-05T23:01:58.913 回答
0

在 Index.aspx 中做

     <% foreach(BookingTableSet set in Model)
     {
        Html.Render("view1", set);
     }%>

再次记住,您的 view1 也有一个母版页,其中包含 html 标记,Index.aspx 也是如此。即使浏览器会呈现它,这也不是好的 html。您应该为 view1 创建一个局部视图,然后将该局部视图包含在您的视图中(Index.aspx)

于 2012-06-06T17:04:23.130 回答