我正在关注 Book Professional ASP.NET MVC 2 中的 NerdDinner 部分。目前我处于需要实现 DinnerFormViewModel 和 Renderpartial Dinnerform 的部分。这本书在这里包含一些错误,所以我尝试在互联网上搜索并自己修复它。
我已将 DinnerFormViewModel 放在 Models 文件夹中,这是代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace NerdDinner.Models
{
public class DinnerFormViewModel : Controller
{
private static string[] _countries = new[]{
"USA",
"Ireland",
"Scotland",
"Namibia"
};
//Properties
public Dinner Dinner { get; private set; }
public SelectList Countries { get; private set; }
//Constructor
public DinnerFormViewModel(Dinner dinner)
{
Dinner = dinner;
Countries = new SelectList(_countries, dinner.Country);
}
// GET: /DinnerFormViewModel/
public ActionResult Index()
{
return View();
}
}
}
然后我制作了 DinnerForm.ascx(部分类):
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewPage<NerdDinner.Models.DinnerFormViewModel>" %>
<%: Html.ValidationSummary("Please correct the errors and try again.") %>
<% using (Html.BeginForm()) { %>
<fieldset>
<p>
<%: Html.LabelFor(m => m.Dinner.Title) %>
<%: Html.TextBoxFor(m => m.Dinner.Title) %>
<%: Html.ValidationMessageFor(m => m.Dinner.Title, "*") %>
ETC...
我做了如下的edit.aspx:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<NerdDinner.Models.DinnerFormViewModel>" %>
<asp:Content ID="Title" ContentPlaceHolderID="TitleContent" runat="server">
Edit: <%: Model.Dinner.Title %>
</asp:Content>
<asp:Content ID="Edit" ContentPlaceHolderID="MainContent" runat="server">
<h2>Edit Dinner</h2>
<% Html.RenderPartial("DinnerForm"); %>
</asp:Content>
现在,如果我启动应用程序,<% Html.RenderPartial("DinnerForm"); 处会出现错误 %> 会弹出说: c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\c8cca855\23406a1e\App_Web_dinnerform.ascx.32d6c807.tczxq3bd.0.cs(166): error CS0030 :无法将类型“ASP.views_dinners_dinnerform_ascx”转换为“System.Web.Mvc.ViewUserControl”
我认为它与命名空间有关,但我无法修复错误,有人遇到同样的问题或有人可以帮助我吗?谢谢!:)