Question

I have a view that was using a standard Model but now I need to add information from two different models on the page. I created a ViewModel after doing some research to handle this. Now I am getting an error when tring to show my view saying:

DataBinding: 'VirtualAuthtech.ViewModels.CMSCalculatorViewModel' does not contain a property with the name 'WORKCY'.

Here's my viewmodel

namespace VirtualAuthtech.ViewModels
{
    public class CMSCalculatorViewModel
    {
        public CPT CPT { get; set; }
        public GPCI GPCI { get; set; }
    }
}

CPT is the first model I was originally using and then I need to add GPCI

Here is the ActionResult for my view

public ActionResult _CMSCalculator()
        {
            string CPTCode = string.Empty;
            string MOD = string.Empty;
            string GPCIPayID = "31146";
            string GPCIYear = "2011";
            if (Request.Params["CPT1"] != null)
            {
                CPTCode = Request.Params["CPT1"];
                MOD = Request.Params["MOD"];
            }
            return PartialView("_CMSCalculatorPanel", (string.IsNullOrEmpty(CPTCode) ? null as CMSCalculatorViewModel : CPTDataHelper.GetCPTGPCI(CPTCode, MOD, GPCIPayID, GPCIYear)));
        }

Here is the method to get the data from my models

    public static CMSCalculatorViewModel GetCPTGPCI(string CPTCode, string MOD, string GPCIPayID, string GPCIYear)
    {
        using (var DB = new VADataContext())
        {
            var view = new CMSCalculatorViewModel();
            if (String.IsNullOrWhiteSpace(MOD))
            {
                view.CPT = (from cpt in DB.CPTs
                            where cpt.CPT1 == CPTCode
                            select cpt).FirstOrDefault<CPT>();
                view.GPCI = (from gpci in DB.GPCIs
                             where gpci.PAYID == GPCIPayID && gpci.YEAR == GPCIYear
                             select gpci).FirstOrDefault<GPCI>();
            }
            else
            {
                view.CPT = (from cpt in DB.CPTs
                            where cpt.CPT1 == CPTCode && cpt.MOD == MOD
                            select cpt).FirstOrDefault<CPT>();
                view.GPCI = (from gpci in DB.GPCIs
                             where gpci.PAYID == GPCIPayID && gpci.YEAR == GPCIYear
                             select gpci).FirstOrDefault<GPCI>();
            }
            return view;
        }
    }

Now my view is using the new ViewModel and I'm trying to display a field:

<div style='float:left; width: 35px; margin: 0; padding: 0; text-align: center; color: #094ab2'>@VirtualAuthtech.CallbackPanelHelper.GetFieldNumber(Model, "WORKCY")</div>

And I'm getting an error in this piece of code:

public static double GetFieldNumber(object data, string fieldName)
        {
            object text = DataBinder.Eval(data, fieldName);
            if (text == null || text.ToString() == string.Empty)
                return 0;
            double number = Convert.ToDouble(text);
            return number;
        }

It's saying my model does not contain the property of the field I'm looking for. Do I need to tell it to look in the CPT part of my view model?

This is the first ViewModel I've ever created so I might be going about this all wrong. Any help to send me in the right direction would be greatly appreciated!

Was it helpful?

Solution

Why the call to GetFieldNumber? What would be wrong with the following line:

<div style='float:left; width: 35px; margin: 0; padding: 0; text-align: center; color: #094ab2'>@Model.CPT.WORKCY</div>

But if you have to use run-time binding, then maybe the line in your function should be:

object text = DataBinder.Eval((CMSCalculatorViewModel)data.CPT, fieldName);

OTHER TIPS

I was able to get it working using this code.

<div style='float:left; width: 35px; margin: 0; padding: 0; text-align: center; color: #094ab2'>@if(Model == null) { <text>0</text> } else { @VirtualAuthtech.CallbackPanelHelper.GetFieldNumber(Model.CPT, "WORKCY") }</div>

However, I have a lot of other fields in this view. I would rather not have to check if the model is null on every one of them. If you have another solution please share.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top