编辑解决方案:
答案可以在这里找到:
http://forums.asp.net/p/1794394/4941748.aspx/1?p=True&t=634704481730535356
但也请参阅里卡多的以下内容。
我有一个名为LedgerUser的控制器/视图。我有一个名为LedgerViewModel的 ViewModel ,它包含一个用于 UserType 实例的 LedgerUser 和 SelectList 实例,以及一个用于图像的名为 UniqueId 的属性。
现在,当我从创建视图中将表单发布回来时,我收到以下错误:
传入字典的模型项的类型为“Accounts.Models.LedgerUser”,但该字典需要“Accounts.ViewModels.LedgerUserViewModel”类型的模型项。说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.InvalidOperationException:传递到字典的模型项的类型为“Accounts.Models.LedgerUser”,但此字典需要“Accounts.ViewModels.LedgerUserViewModel”类型的模型项。
现在我的理解是您将模型传递回操作方法而不是视图模型?我使用以下技术:
- ASP.NET MVC3
- Entity Framework 4数据库优先
我的代码是:
分类帐用户视图模型:
/// <summary>
/// ViewModel to represent the LedgerUser & its required fields.
/// </summary>
public class LedgerUserViewModel
{
public SelectList UserTypes { get; set; }
public string UserType { get; set; }
public LedgerUser LedgerUser { get; set; }
public string UniqueKey { get; set; } //--Used for the Images.
public bool Thumbnail { get; set; }
}
我扩展了LedgerUser模型以使用数据注释进行装饰:
[MetadataType(typeof(LedgerUserMetaData))]
public partial class LedgerUser
{
public class LedgerUserMetaData
{
[Required(ErrorMessage = "Date Of Birth Required")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = " {0:dd/MM/yyyy}")]
[DataType(DataType.Date)]
public object DateOfBirth { get; set; }
}
}
我的LedgerUser的 GET 操作方法:
// GET: /LedgerUser/Create
/// <summary>
/// Action Method to create the LedgerUser. Usually this will be once a user has registered
/// and will be directed from the AccountController.
/// </summary>
/// <param name="id">id</param>
/// <returns></returns>
public ActionResult Create(string id)
{
var uniqueKey = new Guid(id);
var userTypes = new SelectList(db.UserTypes, "id", "Description");
var ledgerUser = new LedgerUser()
{
id = uniqueKey,
RecordStatus = " ",
CreatedDate = DateTime.Now,
DateOfBirth = DateTime.Today
};
var viewModel = new LedgerUserViewModel()
{
UserTypes = userTypes,
LedgerUser = ledgerUser
};
return View(viewModel);
}
我的LedgerUser的 POST 操作方法:
[HttpPost]
public ActionResult Create(bool Thumbnail,LedgerUser ledgeruser, HttpPostedFileBase imageLoad2)
{
///---code to do stuff..
}
我的创建视图:
@model Accounts.ViewModels.LedgerUserViewModel
@using Accounts.Models
@using (Html.BeginForm("Create", "LedgerUser", new { Thumbnail = true}, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Ledger User</legend>
<div class="editor-field">
@Html.HiddenFor(model => model.LedgerUser.id)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LedgerUser.AccountNumber,"Account Number")
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LedgerUser.AccountNumber)
@Html.ValidationMessageFor(model => model.LedgerUser.AccountNumber)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LedgerUser.FirstName,"First Name")
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LedgerUser.FirstName)
@Html.ValidationMessageFor(model => model.LedgerUser.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LedgerUser.LastName,"Last Name")
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LedgerUser.LastName)
@Html.ValidationMessageFor(model => model.LedgerUser.LastName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LedgerUser.DateOfBirth,"D.O.B.")
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LedgerUser.DateOfBirth)
@Html.ValidationMessageFor(model => model.LedgerUser.DateOfBirth)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LedgerUser.UserType, "User Type")
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.LedgerUser.UserType,Model.UserTypes)
</div>
<div class="editor-label">
@Html.Label("Avatar")
</div>
<div class="editor-field">
@Html.UploadImageFor(model => model.UniqueKey,thumbnail:true)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
现在我已经使用 Fiddler 询问了 POST,我发现该名称被正确设置为“LedgerUser”。
内容处置:表单数据;name="LedgerUser.id"
d1cd8e85-700d-4462-aa95-7428dbf58deb -----------------------------7dc963b2304b4 内容处置:表单数据;name="LedgerUser.AccountNumber"
1 -----------------------------7dc963b2304b4 内容处置:表单数据;name="LedgerUser.FirstName"
Gareth -----------------7dc963b2304b4 内容处置:表单数据;name="LedgerUser.LastName"
布拉德利 -----------------7dc963b2304b4 内容处置:表单数据;name="LedgerUser.DateOfBirth"
2012 年 4 月 12 日 ----------------- 7dc963b2304b4 内容处置:表单数据;name="LedgerUser.UserType"
b8502da9-3baa-4727-9143-49e33edc910c -----------------------------7dc963b2304b4 内容处置:表单数据;名称=“图像加载2”;文件名="001.jpg" 内容类型:image/jpeg
我不知所措。多谢你们