![](/img/trans.png)
[英]ASP.Net MVC Error Validation - How to display validation message when passing a custom view model to a view
[英]Error while passing view model to view in ASP.NET MVC
我的觀點是這樣的:
@model SystemBase.Domain.ViewModel.SelfDeclarationReportViewModel
@{
ViewBag.Title = "testi";
Layout = "~/Areas/AdminAgency/Views/Shared/_Layout.cshtml";
}
我想使用以下代碼將聲明的視圖 model class 的一些數據傳遞給我的視圖:
public ActionResult Index(Guid companyID, Guid periodID, EvaluationType evaluationType)
{
try
{
var form = _FormsService.GetMandatory(companyID,
periodID, EvaluationType.Mandatory);
var userForm = _UserFormService.GetMandatoryWithSelfDeclarationScores(
User.UserID().Value, companyID, periodID);
if (userForm == null)
{
userForm = _UserFormService.CreateMandatoryUserForm(form,
companyID, User.UserID().Value);
}
var m = new SelfDeclarationReportViewModel();
m.Form = form;
m.Userform = userForm;
return View(m);
}
catch (Exception ex)
{
throw ex;
}
}
在調試模式下,一切似乎都正常,斷點在視圖和 controller 中都有效,但是 output 是此消息的錯誤
傳入字典的 model 項的類型為“SystemBase.Domain.ViewModel.SelfDeclarationReportViewModel”,但該字典需要“SystemBase.Domain.ViewModel.OrganizationViewModel”類型的 model 項。
您忘記了屬於索引視圖的返回一個返回視圖
索引.cshtml
try
{
var studentViewModel = new StudentViewModel();
return View(studentViewModel);
}
catch
{
}
return View(); //here you need to return something other wise you get an error
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.