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>
开发者_如何学C<% 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>
Your 'Create' action is going to load "Create.aspx" not "Edit.aspx", I suspect that is still pointing to Dinner as the model.
Add the correct name of the view to the ViewResult.
public ActionResult Create()
{
Dinner dinner = new Dinner()
{
EventDate = DateTime.Now.AddDays(7)
};
return View("Edit", new DinnerFormViewModel(dinner));
}
@Lazarus: Good option! @Matthew: you too! @FullmetalBoy: I think you just new in ASP.NET MVC. You only need create the action in controller, after that Right click on this action and creating the View, so that you will never wrong mapping between View and Controller. In the asp.net mvc home page is also have this tutorial. Hope this tips useful to you, man! IMHO.
精彩评论