简体   繁体   English

来自模型的数据未传递到视图

[英]Data from model not passing to view

I have some code that is functioning oddly and was wondering if anyone else hase come across this issue. 我有一些代码运行异常,想知道是否还有其他人遇到此问题。

I have a view model that collects data from a database via a stored procedure and a vb object (no I do not know vb this is legacy) 我有一个视图模型,该模型通过存储过程和vb对象从数据库收集数据(不,我不知道vb是旧版)

When I execute the program the data is collected as expected via the controller. 当我执行程序时,将通过控制器按预期收集数据。 When I debug it I can see all of my parameters populating with information. 当我调试它时,我可以看到我的所有参数都填充了信息。 However when it comes to the view it says that the parameters are null. 但是,当涉及到视图时,它说参数为空。 I have included my code 我已经包含了我的代码

Models: 楷模:

    public class PersonIncomeViewModel
{
    public string IncomeTypeDesc { get; set; }
    public string IncomeDesc { get; set; }
    public string Income { get; set; }
}

public class PersonIncomeListViewModel
{
    public int? PersonId { get; set; }
    public List<PersonIncomeListItem> Incomes { get; set; }

    public PersonIncomeListViewModel()
    {
        Incomes = new List<PersonIncomeListItem>();
    }
}

    public class PersonLookupViewModel : Queue.QueueViewModel
{
    public int Action { get; set; }
    public bool ShowAdvancedFilters { get; set; }

    //Person Search Variables
    [Display(Name = @"Search")]
    public string SpecialSearch { get; set; }
    [Display(Name = @"Person Id")]
    public int? PersonId { get; set; }
    [Display(Name = @"Full Name")]
    public string FullName { get; set; }
    [Display(Name = @"SSN")]
    public string SSN { get; set; }
    public string AddressStatus { get; set; }
    public string EmploymentStatus { get; set; }

    public PersonIncomeViewModel Income { get; set; }
    public List<PersonIncomeListItem> Incomes { get; set; } 



    public PersonLookupViewModel()
    {

        Income = new PersonIncomeViewModel();
        Incomes = new List<PersonIncomeListItem>();

    }

}

Controller: 控制器:

public ActionResult _Income(int id)
    {
        var vm = new PersonLookupViewModel();

        var personManager = new dtPerson_v10_r1.Manager( ref mobjSecurity);

        //var person = personManager.GetPersonObject((int)id, vIncludeIncomes: true);
        var person = personManager.GetPersonObject(id, vIncludeIncomes: true);

        var look = JsonConvert.SerializeObject(person.Incomes);

        foreach (dtPerson_v10_r1.Income income in person.Incomes)
        {

            if (income.IncomeType_ID == 0)
            {
                var item = new PersonIncomeListItem
                               {
                                   IncomeTypeDesc = "Unknown",
                                   IncomeDesc = income.IncomeDesc,
                                   Income = mobjFormat.FormatObjectToCurrencyString(income.Income)
                               };
                vm.Incomes.Add(item);
            }

            if (income.IncomeType_ID == 1)
            {
                var item = new PersonIncomeListItem
                {
                    IncomeTypeDesc = "Alimony",
                    IncomeDesc = income.IncomeDesc,
                    Income = mobjFormat.FormatObjectToCurrencyString(income.Income)
                };

                vm.Incomes.Add(item);
            }

            if (income.IncomeType_ID == 2)
            {
                var item = new PersonIncomeListItem
                {
                    IncomeTypeDesc = "Child Support",
                    IncomeDesc = income.IncomeDesc,
                    Income = mobjFormat.FormatObjectToCurrencyString(income.Income)
                };

                vm.Incomes.Add(item);
            }

        }

        return PartialView(vm);
    }

View: 视图:

@using dtDataTools_v10_r1
@using ds_iDMS.Models.Person
@model ds_iDMS.Models.Person.PersonLookupViewModel

@{
    var format = new dtDataTools_v10_r1.CustomFormat();

    var newInitials = (Model.Income.IncomeTypeDesc.First().ToString() + Model.Income.IncomeDesc.First().ToString() + Model.Income.Income.First().ToString()).ToUpper();
}    

    using (Html.DSResponsiveRow(numberOfInputs: ExtensionMethods.NumberOfInputs.TwoInputs))
    {
        using (Html.DSCard(ExtensionMethods.Icon.CustomText, iconInitials: newInitials, color: ExtensionMethods.Colors.PrimaryBlue))
        {
            <div>@Model.Income.IncomeTypeDesc</div>
            <div>@Model.Income.IncomeDesc</div>
            <div>@Model.Income.Income</div>
        }
    }

There are some extensions that we have built but they are irrelevant to the issue 我们已经构建了一些扩展,但与该问题无关

The line that errors out is this one: 出错的行是这一行:

var newInitials = (Model.Income.IncomeTypeDesc.First().ToString() + Model.Income.IncomeDesc.First().ToString() + Model.Income.Income.First().ToString()).ToUpper(); var newInitials =(Model.Income.IncomeTypeDesc.First()。ToString()+ Model.Income.IncomeDesc.First()。ToString()+ Model.Income.Income.First()。ToString())。ToUpper() ;

Which drives all of the extension methods on the view and as I run the debugger over it all of the parameters read null, however like I said when I run the debugger and check them in the controller they are populated properly. 它驱动视图上的所有扩展方法,并且当我在其上运行调试器时,所有参数均读取为空,但是就像我说的那样,当我运行调试器并在控制器中检查它们时,它们已正确填充。

Sorry about the long post but I wanted to ensure all the detail was there 抱歉,很长的帖子,但我想确保所有细节都在那里

This is how to pass the Object model to your Partial View 这是将Object model传递到Partial View

return PartialView("YourViewName", vm);

or using the Views path 或使用Views路径

return PartialView("~/YourView.cshtml", vm);

EDIT 编辑

Try starting your Action Method like this 尝试像这样启动您的操作方法

    var vm= new Person();
    vm.PersonLookupViewModel = new PersonLookupViewModel();

Problem solved I had issues with some of my vb objects and had the vb person take a look at them and she fixed them. 解决了问题我的某些vb对象出现问题,让vb人员查看了一下,然后她修复了它们。

Thank you for all the help 谢谢你的帮助

EDIT What had to happen is the vb object had to be re-written and my logic was just fine as it was in the beginning. 编辑必须发生的是,必须重写vb对象,而我的逻辑与刚开始时一样。 I marked the one response to my question as the answer because had it been in true MVC without vb objects attached to it, that would have worked perfectly 我将对我的问题的一个回答标记为答案,因为如果它是真正的MVC,而没有附加vb对象,那将是完美的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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