簡體   English   中英

傳遞視圖 model 以在 ASP.NET MVC 中查看時出錯

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM