Я новичок, и я попытался найти решение этого сообщения об ошибке:
"Элемент модели передан в
словарь имеет тип
'NerdDinner.Controllers.DinnerFormViewModel',
но этот словарь требует модели
тип предмета
"NerdDinner.Models.Dinner"
Есть похожая ошибка проблемы, но не та же самая проблема контекста.
Я считаю, что проблема находится в edit.aspx, и все же я не могу ее решить.
DinnersController:
//
// GET: /Dinners/Create
public ActionResult Create()
{
Dinner dinner = new Dinner()
{
EventDate = DateTime.Now.AddDays(7)
};
return View(new DinnerFormViewModel(dinner));
}
Модель:
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>