Data Grid doesn't send data

Jun 22, 2011 at 8:05 AM

Hi,

I am having trouble with a data grid of mine. 

I have a View Model with a List<Tracker<PortfolioAllocation>> property on it which I load via a submit back to the server.  This then sets the grid on the client perfectly.  The user then has the option to modify the grid or just submit the page.

However when I submit the page the grid property is always null where-as all the other propertys on the View Model are populated.  I check the Request.Form collection and I can see the Grid things in there like PortfolioAllocation.$$0 so I assume they are being set through to the server fine, plus they show on the client which is based on this property.

How come on the submission on the page the grid items are not put in to the View Models List<Tracker<PortfolioAllocation>> property? 

The DataGrid is setup as:

@Html.DataGridFor(model => model.PortfolioAllocations,

ItemContainerType.tr, null, "PortfolioSetDisplayItem", "PortfolioSetGrid")

Thanks,

Craig

Coordinator
Jun 22, 2011 at 8:48 AM

Often you get a null model whem there is a mismatch between the model used to fill the view and the model used to receive posted data in an action method. The name of the properties that one want to match MUST be the same, They MUST be at the same level in the model tree, and they MUST have the same type.

However I see you passed a null edit template. If you don't want the user modify existing row but just add new rows, you need to pass the edit template anyway and just omit the button to go to edit mode, because the edit template is used also to insert a new row.

Add the edit template and see what happens. If you still have problems, please show me:

1) Action method that receives the data. I need just name and parameters.

2) Viewmodel used by the action method that receives the data.

 

Jun 22, 2011 at 8:46 PM
Edited Jun 22, 2011 at 9:08 PM

I don't think its a model problem at all.

If I just load the PortfolioAllocations and display them it looks fine.  I submit and they are empty.  However if I do a modification while it is on the view (like deleting one) then submit that item I changed is posted back to the server but the rest of the items are not.

It states in the documentation for the grid that:

  • When the Item is deleted Value is set to null but OldValue contains the old data to be deleted in the database. The change is signalled by setting Changed to true.

    This is not the case for me.  I delete an item and the OldValue is null and the Value contains the deleted item.

    Is there something I can do to flag all the items as changed when it is on the View?

  • Action method for receiving the request is

    public ActionResult Dividend(string submitButton, Dividend  dividend)

  • View Model is:

  •  

    public class Dividend

  • {

  • public string TransactionType { get; set ; }

    [Required]

    [Display(Name = "Asset Code")]

     public string Asset { get; set ; }

    [Required ]

    [Display(Name = "Ex-Dividend Date" )]

     public DateTime TransactionDate { get; set

    ; }

    [Required

    ]

    [Display(Name = "Payment Date"

    )]

     public DateTime SettlementDate { get; set

    ; }

    [Required

    ]

    [Display(Name = "Settlement Currency"

    )]

     public string SettlementCurrency { get; set

    ; }

    [Display(Name = "Dividend Currency"

    )]

     public string BaseCurrency { get; set

    ; }

    [Required

    ]

    [Display(Name = "Settlement FX Rate"

    )]

     public double SettlementFXRate { get; set

    ; }

    [Required

    ]

    [Display(Name = "Dividend"

    )]

     public double Price { get; set

    ; }

    [Display(Name = "Debtor Account"

    )]

     public string Broker { get; set

    ; }

    [Display(Name = "Dividend Withholding Rate"

    )]

     public double WithholdingRate { get; set

    ; }

     public List<Tracker<PortfolioAllocation>> PortfolioAllocations { get; set

    ; }
  • }

  •  

    Craig

  • Coordinator
    Jun 23, 2011 at 8:16 AM

    Hi Craig,

    I have not understood what your settings are, because you are speaking of editing data but your datagrid has no edit template. Moreover, your action method that receive the post has no list to  update but just a single item. Thus I suspect you are updating your data with a detail form attached to the grid with a detail link.

    Is this the case?

    If yes consider what follows:

     

    The detail form just update a single item via ajax, the it shows the updated field on the grid. However, the detail form just show a field has been updated outside the grid. IT DOESN't change the Value of the grid row fileds, that remains equal to the old values.

    That said...there are things happening to your application that are not explained ...also by my hypothesis

    About setting the Changed bit of a record you can set it in javascript, however it is mantained by the grid so there is always the risk it is reset to false. Moreover, it needs to be coordinated with OldValue and Value...so changing it  independently may cause bugs. However, why do you want to change it. It is changed automatically when you go in edit mode and type something.

     

    If you want you can send me a project so I can seet better the code and understand what is going on(as usual no database)...you already know how to do :)

    Jun 23, 2011 at 7:34 PM
    Edited Jun 23, 2011 at 7:42 PM

    I can edit the grid without an Edit template by using the "Delete" button on the Display Grid.  This will remove the item from the grid...doing this sets the OldValue to NULL and the NewValue to the deleted item (weird), and is against what is in the documentation.

    My list is contained in the Dividend object as I showed in my prior post:

    public List<Tracker<PortfolioAllocation>> PortfolioAllocations { get; set; }

    This list is shown on the Grid by using a m=>m.PortfolioAllocations

    Are you saying that I need a specific list attribute to be in the action?  I don't see why.

    All of this is contained on the single page.  No Details form.

    Craig

    Jun 23, 2011 at 8:22 PM
    Edited Jun 23, 2011 at 8:32 PM

    I am seriously missing something here.  I edited your example and made it do the same thing that I am doing and it works fine.  I can't see what I'm doing wrong with my example though....

    Craig

    Coordinator
    Jun 23, 2011 at 10:53 PM

    Now I understood you settings. Sorry but your model wasn't well fromatted and I have not seen the collections at the end.

    The rule is "if it seems something magis is happening than for susre we made a trivial mistake".

    So let do a simple check:

    1) Is this a new project? If yes have you remembered to install properli the mvc controls toolkit in the global.asax?

    2) The MVC model binder may  fail if a property a its descendant in the model tree have the same name, for instance Plant.Device.AssociatedPlant.Device may fail binding because there is a Device that is a descendant of another Device property.

    3) Are all property of PortFolio allocation declared as public?

     

    May I see  the  PortfolioAllocation class? 

     

    I hope I hit the problem...anyway if you are not able to solve pls send me a test project, I don't exclude the possibility of a rather involved bug also if I think it is very unlikely, because I built a test similar to your problem and it works properly.

    In the worst case I can put abreakpoint in the sources of the model binder ..to see what happens :)

    Jun 23, 2011 at 11:50 PM

    AHH, just after I sent through the code I have found the issue.

    When loading up the Tracker<PortfolioAllocation> I was only setting the .Value of it.  I see from your example you need to also set the .OldValue and the .Changed to false.

    Doing this fixed the problem.

    Cheers,

    Craig

    Coordinator
    Jun 24, 2011 at 2:26 AM

    Mvc is quite more complex than web forms. It is difficult to be able to use new features just reading the doc. I suggest to follows the tutorials, and using them as guidelines, at least the first times. 

    Anyway you are acquiring a good experience of Mvc and Mvc Controls Toolkit. Trial and errors is the best way to learn. :)