尝试实现部分表单时,名称空间'NerdDinner.Models'中不存在类型或命名空间名称'DinnerForm'

时间:2011-09-28 15:06:10

标签: asp.net-mvc nerddinner

描述:编译服务此请求所需的资源时发生错误。请查看以下特定错误详细信息并相应地修改源代码。

编译器错误消息:CS0234:名称空间'NerdDinner.Models'中不存在类型或命名空间名称'DinnerForm'(您是否缺少程序集引用?)

来源错误:

第170行:
第171行:[System.Runtime.CompilerServices.CompilerGlobalScopeAttribute()] 第172行:public class views_dinners_create_aspx:System.Web.Mvc.ViewPage,System.Web.SessionState.IRequiresSessionState,System.Web.IHttpHandler { 第173行:
第174行:private static bool @__ initialized;

DinnerFormViewModel.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NerdDinner.Controllers;



namespace NerdDinner.Models
{
public class DinnerFormViewModel
{
    // Properties
    public Dinner Dinner { get; private set; }
    public SelectList Countries { get; private set; }

    // Constructor
    public DinnerFormViewModel(Dinner dinner)
    {
        Dinner = dinner;
        Countries = new SelectList(PhoneValidator.Countries, dinner.Country);
    }
}
}

DinnerForm.ascx:

<%@ Control Language="C#"       Inherits="System.Web.Mvc.ViewUserControl<NerdDinner.Controllers.DinnerFormViewModel>" %>

<%= Html.ValidationSummary("Please correct the errors and try again.") %>

<% using (Html.BeginForm()) { %>

<fieldset>
<p>
<label for="Title">Dinner Title:</label>
<%= Html.TextBox("Title", Model.Dinner.Title) %>
<%=Html.ValidationMessage("Title", "*") %>
</p>
<p>
<label for="EventDate">Event Date:</label>
<%= Html.TextBox("EventDate", Model.Dinner.EventDate) %>
<%= Html.ValidationMessage("EventDate", "*") %>
</p>
<p>
<label for="Description">Description:</label>
<%= Html.TextArea("Description", Model.Dinner.Description) %>
<%= Html.ValidationMessage("Description", "*") %>
</p>
<p>
<label for="Address">Address:</label>
<%= Html.TextBox("Address", Model.Dinner.Address) %>
<%= Html.ValidationMessage("Address", "*") %>
</p>
<p>
<label for="Country">Country:</label>
<%= Html.DropDownList("Country", Model.Countries) %>               
<%= Html.ValidationMessage("Country", "*") %>
</p>
<p>
<label for="ContactPhone">Contact Phone #:</label>
<%= Html.TextBox("ContactPhone", Model.Dinner.ContactPhone) %>
<%= Html.ValidationMessage("ContactPhone", "*") %>
</p>

<p>
<input type="submit" value="Save"/>
</p>
</fieldset>

<% } %>

DinnersControllers.cs(创建方法)

//
// GET: /Dinners/Create

    public ActionResult Create()
    {

        Dinner dinner = new Dinner()
        {
            EventDate = DateTime.Now.AddDays(7)
        };
        return View(new DinnerFormViewModel(dinner));
    }

    //
    // POST: /Dinners/Create

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(Dinner dinner)
    {

        if (ModelState.IsValid)
        {

            try
            {
                dinner.HostedBy = "SomeUser";

                dinnerRepository.Add(dinner);
                dinnerRepository.Save();

                return RedirectToAction("Details", new { id = dinner.DinnerID });
            }
            catch
            {
                foreach (var issue in dinner.GetRuleViolations())
                {
                    ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
                }

                return View(new DinnerFormViewModel(dinner));
            }
        }

        return View(new DinnerFormViewModel(dinner));
    }

Create.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"   Inherits="System.Web.Mvc.ViewPage<NerdDinner.Models.DinnerForm>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Host A Dinner
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Host a Dinner</h2>

<% Html.RenderPartial("DinnerForm"); %>
</asp:Content>

1 个答案:

答案 0 :(得分:1)

<强> DinnerForm.ascx:

Inherits="System.Web.Mvc.ViewUserControl<NerdDinner.Models.DinnerFormViewModel>