简体   繁体   中英

Error while passing view model to view in ASP.NET MVC

My view looks something like this:

@model SystemBase.Domain.ViewModel.SelfDeclarationReportViewModel
@{
    ViewBag.Title = "testi";
    Layout = "~/Areas/AdminAgency/Views/Shared/_Layout.cshtml";
}

I want to pass some data of a declared view model class to my view using this code:

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;
    }
}

In debugging mode, everything seems ok and the breakpoints work in both view and controller, but the output is an error with this message

The model item passed into the dictionary is of type 'SystemBase.Domain.ViewModel.SelfDeclarationReportViewModel', but this dictionary requires a model item of type 'SystemBase.Domain.ViewModel.OrganizationViewModel'.

you forgot the return one return view which belong to the Index View

Index.cshtml

            try
            {
                var studentViewModel = new StudentViewModel();
                
                return View(studentViewModel);
            }
            catch
            {

            }
            return View();        //here you need to return something other wise you get an error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM