我创建了一个视图模型以及关联的控制器和视图,但是看起来我的头和代码都缺少了一部分。提交页面时,无论是否更改数据库,我都会得到以下信息。 “实体类型ReportReviewViewModel不是当前上下文模型的一部分。”
模型(并非所有字段)
public partial class RFDS
{
public System.Guid rfds_processing_id { get; set; }
public string location_name { get; set; }
public string address { get; set; }
}
public partial class Sub_Clients_Xref
{
public int id { get; set; }
public string sub_client { get; set; }
public string sub_client_attn { get; set; }
public string sub_client_address { get; set; }
}
public partial class Engineering_License_Xref
{
public int id { get; set; }
public string state_name { get; set; }
public string license_number { get; set; }
}
ViewModel
public class ReportReviewViewModel
{
public RFDS rfds { get; set; }
public Sub_Clients_Xref subclient { get; set; }
public Engineering_License_Xref engineer { get; set; }
public States_Xref state { get; set; }
}
控制器
public ActionResult Review(Guid? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
RFDS rfds_db = db.RFDS.Find(id);
if (rfds_db == null)
{
return HttpNotFound();
}
List<RFDS> rfds1 = db.RFDS.ToList();
List<Sub_Clients_Xref> subclients = db.Sub_Clients_Xref.ToList();
List<Engineering_License_Xref> engineers = db.Engineering_License_Xref.ToList();
List<States_Xref> states = db.States_Xref.ToList();
var viewModel = (from r in rfds1
join s in subclients on r.sub_client equals s.sub_client
join st in states on r.state equals st.state
join e in engineers on r.engineer_name equals e.engineer_name
where r.rfds_processing_id == id && r.sub_client == s.sub_client &&
(r.engineer_name == e.engineer_name && e.state_name == st.state_name)
select new ReportReviewViewModel
{
rfds = r,
subclient = s,
engineer = e
});
return View(viewModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Review(ReportReviewViewModel viewModel)
{
if (ModelState.IsValid)
{
db.Entry(viewModel).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Review");
}
return View(viewModel);
}
UPDATED CONTROLLER
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Review(ReportReviewViewModel viewModel)
{
if (ModelState.IsValid)
{
db.Entry(viewModel.rfds).State = EntityState.Modified;
db.Entry(viewModel.engineer).State = EntityState.Modified;
db.Entry(viewModel.subclient).State = EntityState.Modified;
db.Entry(viewModel.state).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Review");
}
VIEW example
@model IEnumerable<TeamWeb.ViewModels.ReportReviewViewModel>
@{
ViewBag.Title = "Review";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="container2">
<div class="nav col-md-3">
@Html.ActionLink("BACK TO LIST", "Index")
</div>
<hr />
<table class="rev-table-main" border="0">
<!----------------------------- EDIT DATA ----------------------------->
@using (Html.BeginForm("Review", "RFDS", FormMethod.Post))
{
@Html.AntiForgeryToken()
foreach (var item in Model)
{
@Html.HiddenFor(model => item.rfds.rfds_processing_id)
<tr>
<td class="rev-values-td">
<table class="rev-table-values" border="0">
@*<tr class="rev-tr">
<td> </td>
</tr>*@
<tr class="rev-tr">
<td style="font-size:medium;font-weight:600;text-align:center;" colspan="2">
Edit Report Information
</td>
</tr>
<tr>
<td style="text-align:right;" width="40%">
Site Name:
</td>
<td width="60%">
@Html.EditorFor(model => item.rfds.location_name)
</td>
</tr>
<tr>
<td align="right">
FA Location Code:
</td>
<td>
@Html.EditorFor(model => item.rfds.fa_location_code)
</td>
</tr>
<tr>
<td align="right">
Site Address:
</td>
<td>
@Html.EditorFor(model => item.rfds.address)
</td>
</tr>
<tr>
<td align="right">
Site City:
</td>
<td>
@Html.EditorFor(model => item.rfds.city)
</td>
</tr>
<tr>
<td align="right">
Site State:
</td>
<td>
@Html.EditorFor(model => item.rfds.state)
</td>
</tr>
<tr>
<td align="right">
Site Zip Code:
</td>
<td>
@Html.EditorFor(model => item.rfds.zip_code)
</td>
</tr>
<tr>
<td colspan="2">
<hr />
</td>
</tr>
<tr>
<td align="right">
Sub Client:
</td>
<td>
@Html.EditorFor(model => item.subclient.sub_client)
</td>
</tr>
<tr>
<td align="right">
Sub Client Attn:
</td>
<td>
@Html.EditorFor(model => item.subclient.sub_client_attn)
</td>
</tr>
<tr>
<td align="right">
Sub Client Address:
</td>
<td>
@Html.EditorFor(model => item.subclient.sub_client_address)
</td>
</tr>
<tr>
<td align="right">
Sub Client City:
</td>
<td>
@Html.EditorFor(model => item.subclient.sub_client_city)
</td>
</tr>
<tr>
<td align="right">
Sub Client State:
</td>
<td>
@Html.EditorFor(model => item.subclient.sub_client_state)
</td>
</tr>
<tr>
<td align="right">
Sub Client Zip Code:
</td>
<td>
@Html.EditorFor(model => item.subclient.sub_client_zip)
</td>
</tr>
<tr>
<td colspan="2">
<hr />
</td>
</tr>
<tr>
<td align="right">
Capability %:
</td>
<td>
@Html.EditorFor(model => item.rfds.capability)
</td>
</tr>
<tr>
<td colspan="2">
<hr />
</td>
</tr>
<tr>
<td align="right">
Report Date:
</td>
<td>
@Html.EditorFor(model => item.rfds.submit_date)
</td>
</tr>
<tr>
<td align="right">
Job Number:
</td>
<td>
@Html.EditorFor(model => item.rfds.job_number)
</td>
</tr>
<tr>
<td align="right">
Revision:
</td>
<td>
@Html.EditorFor(model => item.rfds.revision)
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Save" />
</td>
</tr>
</table>
</td>
</tr>
}
}
</table>
</div>