我有MVC 4項目,應該爲投標創建一組記錄,然後保存它應該重定向到某個頁面,我將顯示所有記錄。查看不顯示數據傳遞的數據
下面是我保存後的問題,它會重定向,但從模型傳遞到視圖的數據不會更新/不顯示在視圖上。
這裏是我的控制器動作中獲取數據:
private CoverageViewModel coveragedata = new CoverageViewModel();
private NewProposalDBContext db = new NewProposalDBContext();
//
// GET: /Coverage/
public ActionResult Index()
{
CoverageViewModel coverage = new CoverageViewModel();
List<Institution> institutions = db.Institutions.ToList();
if (coveragedata.Proposal != null)
{
coverage.Institution = institutions;
coverage.Proposal = (List<NewProposal>)Session["coveragedata"];
}
else
{
List<NewProposal> listProposal = new List<NewProposal>();
listProposal = db.NewProposal.ToList();
List<LeadsManagement> listLeadsMgt = new List<LeadsManagement>();
listLeadsMgt = db.LeadsManagements.ToList();
List<LeadEmployees> listLeadsEmployees = new List<LeadEmployees>();
listLeadsEmployees = db.LeadEmployees.ToList();
List<VacsInAction> listVacsInAction = new List<VacsInAction>();
listVacsInAction = db.VacsInActions.ToList();
List<NewProposal> listofImplementedProposal = new List<NewProposal>();
listofImplementedProposal =
db.NewProposal.Where(x => x.Status.StatusID == 3).ToList();
coverage.Institution = institutions;
coverage.Proposal = listProposal;
coverage.LeadsManagement = listLeadsMgt;
coverage.LeadsEmployee = listLeadsEmployees;
coverage.VacsInAction = listVacsInAction;
}
return View(coverage);
}
這裏是我查看 我使用的會話只是爲了讓來自控制器的最新數據,並嘗試覆蓋模式的值。
@{
ViewBag.Title = "Index";
if(Session["coveragedata"] != null)
{
Model.Proposal = (List<ULIV.ViewModels.NewProposal>)Session["coveragedata"];
}
if(Session["leadsMgt"] != null)
{
Model.LeadsManagement = (List<ULIV.Models.LeadsManagement>)Session["leadsMgt"];
}
if(Session["leadsEmp"] != null)
{
Model.LeadsEmployee = (List<ULIV.Models.LeadEmployees>)Session["leadsEmp"];
}
這裏的顯示數據的實際:
@foreach (var proposal in Model.Proposal)
{
if (proposal.Status.StatusID == 1)
{
<tr>
<td align="center">
@Html.DisplayFor(modelItem => proposal.ProposalCode)
</td>
<td align="center">
@Html.DisplayFor(modelItem => proposal.VaccineType.VaccineTypeName)
</td>
<td align="center">
@Html.DisplayFor(modelItem => proposal.Product.ProductName)
</td>
<td align="center">
@Html.DisplayFor(modelItem => proposal.ProposedUnitPrice)
</td>
<td align="center">
@Html.DisplayFor(modelItem => proposal.ProposedVolume)
</td>
<td align="center">
@Html.DisplayFor(modelItem => proposal.Status.StatusName)
</td>
</tr>
}
}
你爲什麼要使用會話?如果您正在重定向,只需創建一個包含新視圖更新數據的模型,並使用模型顯示 –
「if(coveragedata.Proposal!= null)」的含義是什麼? - 它總是**爲空(您每次調用方法時,都會初始化一個新的覆蓋實例!)。並停止使用會話! –