我是新手,我尝试找到此错误消息的解决方案:
“模型项目传入 字典是类型 '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>
答案 0 :(得分:5)
您的“创建”操作将加载“Create.aspx”而非“Edit.aspx”,我怀疑仍然指向Dinner作为模型。
答案 1 :(得分:2)
将正确的视图名称添加到ViewResult。
public ActionResult Create()
{
Dinner dinner = new Dinner()
{
EventDate = DateTime.Now.AddDays(7)
};
return View("Edit", new DinnerFormViewModel(dinner));
}
答案 2 :(得分:0)
@Lazarus:不错的选择! @Matthew:你也是! @FullmetalBoy:我认为你是ASP.NET MVC的新手。您只需要在控制器中创建操作,然后右键单击此操作并创建视图,这样您就不会错误地在View和Controller之间进行映射。在asp.net mvc主页上也有本教程。希望这个技巧对你有用,伙计!恕我直言。