私は ASP.NET MVC 2 と VS 2010 を使用しています。2 つのビューがあります
。view1はデータベースにレコードを作成するためのものです。
view2は、データベースからのデータを一覧表示するためのものです。
これらのビューをマージして、4 つの .aspx ファイルを使用して 1 つの .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>
view1.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");
}
EDIT2:
基本的に私が持っているのは、これらの 2 つのチュートリアルを一緒にまとめたものです
。 -asp-net-mvc-cs
http://jeffreypalermo.com/blog/asp-net-mvc-and-the-templated-partial-view-death-to-ascx/
たぶん、これは私がやろうとしていることを理解するのに役立つでしょう。