[英]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.