No connected between view and controller

I'm a newbie and I tried finding a solution of this error message:

"The model item passed into the dictionary is of type 'NerdDinner.Controllers.DinnerFormViewModel', but this dictionary requires a model item of type 'NerdDinner.Models.Dinner'"

There are similiar problem error but not the same context problem. I believe that the problem is located in the edit.aspx and still i can't solve it.

DinnersController:

//
// GET: /Dinners/Create

public ActionResult Create()
{

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

    return View(new DinnerFormViewModel(dinner));
}

Models:

namespace NerdDinner.Models
{
    [MetadataType(typeof(Dinner_Validation))]
    public partial class Dinner
    {

    }

    public class Dinner_Validation
    {
        [Required(ErrorMessage = "Title is required")]
        [StringLength(50, ErrorMessage = "Title may not be longer than 50 characters")]
        public string Title { get; set; }

        [Required(ErrorMessage = "Description is required")]
        [StringLength(265, ErrorMessage =
          "Description must be 256 characters or less")]
        public string Description { get; set; }

        [Required(ErrorMessage = "Address is required")]
        public string Address { get; set; }

        [Required(ErrorMessage = "Country is required")]
        public string Country { get; set; }

        [Required(ErrorMessage = "Phone# is required")]
        public string ContactPhone { get; set; }
    }
}


namespace NerdDinner.Controllers
{
    public class DinnerFormViewModel
    {

        private static string[] _countries = new[] 
        {
            "USA",
            "Afghanistan",
            "Akrotiri",
            "Albania",
            //... omitted for brevity
            "Zimbabwe"
        };


        // 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);
        }
    }
}

Edit.aspx:

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

<asp:Content ID="Title" ContentPlaceHolderID="TitleContent" runat="server">
   Edit: <%: Model.Dinner.Title %>
</asp:Content>

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

   <h2>Edit Dinner</h2>
   <% using (Html.BeginForm()) { %>
       <%: Html.ValidationSummary("Please correct the errors and try again.") %>
       <fieldset>
          <legend>Fields</legend>
           <div class="editor-label">
               <%: Html.LabelFor(m => m.Dinner.Title) %>
           </div>
           <div class="editor-field">
               <%: Html.TextBoxFor(m => m.Dinner.Title)%>
               <%: Html.ValidationMessageFor(m => m.Dinner.Title, "*")%>
           </div>
           <div class="editor-label">
               <%: Html.LabelFor(m => m.Dinner.EventDate) %>
           </div>
           <div class="editor-field">
               <%: Html.TextBoxFor(m => m.Dinner.EventDate)%>
               <%: Html.ValidationMessageFor(m => m.Dinner.EventDate, "*")%>
           </div>
           <div class="editor-label">
               <%: Html.LabelFor(m => m.Dinner.Description) %>
           </div>
           <div class="editor-field">
               <%: Html.TextAreaFor(m => m.Dinner.Description) %>
               <%: Html.ValidationMessageFor(m => m.Dinner.Description, "*") %>
           </div>
           <div class="editor-label">
               <%: Html.LabelFor(m => m.Dinner.Address) %>
           </div>
           <div class="editor-field">
               <%: Html.TextBoxFor(m => m.Dinner.Address) %>
               <%: Html.ValidationMessageFor(m => m.Dinner.Address, "*") %>
           </div>
<%: Html.LabelFor(m => m.Dinner.Country) %>
<%: Html.DropDownListFor(m => m.Dinner.Country, Model.Countries) %>
<%: Html.ValidationMessageFor(m => m.Dinner.Country, "*") %>
           <div class="editor-label">
               <%: Html.LabelFor(m => m.Dinner.ContactPhone)%>
           </div>
           <div class="editor-field">
               <%: Html.TextBoxFor(m => m.Dinner.ContactPhone)%>
               <%: Html.ValidationMessageFor(m => Model.Dinner.ContactPhone, "*") %>
           </div>
           <div class="editor-label">
               <%: Html.LabelFor(m => m.Dinner.Latitude)%>
           </div>
           <div class="editor-field">
               <%: Html.TextBoxFor(m => m.Dinner.Latitude)%>
               <%: Html.ValidationMessageFor(m => Model.Dinner.Latitude, "*") %>
           </div>
           <div class="editor-label">
               <%: Html.LabelFor(m => m.Dinner.Longitude)%>
           </div>
           <div class="editor-field">
               <%: Html.TextBoxFor(m => m.Dinner.Longitude)%>
               <%: Html.ValidationMessageFor(m => m.Dinner.Longitude, "*")%>
           </div>
           <p>
               <input type="submit" value="Save" />
           </p>
       </fieldset>

   <% } %>

</asp:Content>

Answers


Your 'Create' action is going to load "Create.aspx" not "Edit.aspx", I suspect that is still pointing to Dinner as the model.


Need Your Help

Windows Phone7 Development: Selecting target SDK(OS) version

windows windows-phone-7 windows-phone-7.1

Bit confusing in selecting the target version in Windows phone7 Application development.

Urwid ListBox: How to get fluid focus movement?

python user-interface console curses urwid

I have the following code snippet that shows a list of numbers, and highlights the currently in-focus item: