我有以下ViewModel和相应的两个模型。
我在视图上显示来自此ViewModel的数据,但是当我发布要更新的数据时,会发生以下错误
传递到字典中的模型项的类型是' WebMSM.Models.ComplainDetailsVm',但是这个字典需要一个类型为' WebMSM.Models.REPAIRING'的模型项。
def readData():
myList =[]
myFile = open("football.txt", "r")
for lines in myFile:
league = lines.split()
myList.append(league)
return myList
REPAIRING.cs
public partial class ComplainDetailsVm
{
public virtual REPAIRING REPAIRINGs { get; set; }
public virtual COMPLAIN COMPLAINs { get; set; }
}
COMPLAIN.cs
public partial class REPAIRING
{
[Key]
[DisplayName("JOBSHEET NO")]
public int JOBSHEET_NO { get; set; }
[DisplayName("IN TIME")]
public Nullable<System.DateTime> IN_TIMESTAMP { get; set; }
[DisplayName("CREATE TIME")]
public Nullable<System.DateTime> CREATE_TIMESTAMP { get; set; }
[DisplayName("LAST EDIT TIME")]
public Nullable<System.DateTime> LAST_EDIT_TIMESTAMP { get; set; }
}
控制器行动
public partial class COMPLAIN
{
[Key]
[DisplayName("JOBSHEET NO")]
public int JOBSHEET_NO { get; set; }
[Required]
[DisplayName("COMPANY NAME")]
public string COMPANY_NAME { get; set; }
[Required]
[DisplayName("MODEL NAME")]
public string MODEL_NAME { get; set; }
}
更新
查看
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(int? id,ComplainDetailsVm model)
{
if (ModelState.IsValid)
{
var r = model.REPAIRINGs;
var c = model.COMPLAINs;
db.Entry(r).State = EntityState.Modified;
db.SaveChanges();
}
return View(model);
}
更新增加获取方法
@model WebMSM.Models.ComplainDetailsVm
@{
ViewBag.Title = "EditRepairingComplain";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.REPAIRINGs.JOBSHEET_NO)
@Html.HiddenFor(model => model.COMPLAINs.JOBSHEET_NO)
<div class="form-group">
@Html.LabelFor(model => model.COMPLAINs.COMPANY_NAME,
htmlAttributes: new { @class = "control-label col-md-4" })
<div class="col-md-6">
@Html.TextBoxFor(model => model.COMPLAINs.COMPANY_NAME, new { @class = "form-control", @readonly = "readonly" })
@Html.ValidationMessageFor(model => model.COMPLAINs.COMPANY_NAME, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.COMPLAINs.MODEL_NAME, htmlAttributes: new { @class = "control-label col-md-4" })
<div class="col-md-6">
@Html.TextBoxFor(model => model.COMPLAINs.MODEL_NAME, new { @class = "form-control", @readonly = "readonly" })
@Html.ValidationMessageFor(model => model.COMPLAINs.MODEL_NAME, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.REPAIRINGs.IN_TIMESTAMP, htmlAttributes: new { @class = "control-label col-md-4" })
<div class="col-md-6">
@Html.EditorFor(model => model.REPAIRINGs.IN_TIMESTAMP, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.REPAIRINGs.IN_TIMESTAMP, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.REPAIRINGs.CREATE_TIMESTAMP, htmlAttributes: new { @class = "control-label col-md-4" })
<div class="col-md-6">
@Html.EditorFor(model => model.REPAIRINGs.CREATE_TIMESTAMP, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.REPAIRINGs.CREATE_TIMESTAMP, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.REPAIRINGs.LAST_EDIT_TIMESTAMP, htmlAttributes: new { @class = "control-label col-md-4" })
<div class="col-md-6">
@Html.EditorFor(model => model.REPAIRINGs.LAST_EDIT_TIMESTAMP, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.REPAIRINGs.LAST_EDIT_TIMESTAMP, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-5 col-md-6">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
感谢。
答案 0 :(得分:0)
您可以让View
以两种方式识别您的ViewModel
:您可以为您提供MVC框架,或者您可以使用strongly typed views
在您的情况下,您的视图是强类型的,但引用了错误的对象类。如果您从其他文件复制视图,则会发生这种情况。您应该在cshtml文件中看到以下行:
@model WebMSM.Models.REPAIRING
将其替换为:
@model WebMSM.Models.ComplainDetailsVm
您不应再收到错误。
编辑:
值得一提的是,这些行应位于操作方法返回的cshtml
文件之上。