This project is read-only.

Tip-What to check if a form/action doesn't fire

May 3, 2013 at 9:11 PM
Hi all,
Having just spent a bunch of time trying to track down why an edit form wouldn't show up, I figured it out. So here is a tip to save you debugging time.

In your controller, you define some actions. In my case, it was Edit. The call looks like this:
public ActionResult Edit(string Id)
{
  return View(new CustomerEditViewModel(Id));
}
Pretty straightforward.

Now, in my ListViewModel, I have a private method (Edit) that is passed to the EditCustomer property as a ViewAction. The code for that is also simple:
EditCustomer = new ViewAction(execute: Edit);
Ok, so here is the Edit code (with the error)
private void Edit(IViewAction a, object o)
{
    if (SelectedCustomer == null)
        return;

    var sinks = new ViewResultEventSinks();
    sinks.ViewClosed += (s, e) =>
    {
        Controller.Notification("Refreshing customer list...");
        LoadData();
    };
    Controller.Action("Customer", "Edit", new
    {
        CustomerID = SelectedCustomer.CustomerID
    }, sinks);
}
The field in the table and in the contract, etc. all refer to the field as CustomerID. So this looks just fine.

However, this will result in nothing happening when you double click on the item in the list. The reason is that the variable specified in the Controller.Action MUST match the variable name defined in the controller (Id). Since Id != CustomerID, the parameter is not found and so the method is not considered a match.

So to fix the above, you just need to change the Edit method to use:
  Id = SelectedCustomer.CustomerID
Unfortunately, intellisense can't give you a clue as to what the parameter should be. And since this is part of the code you write, it's hard for the FW to even include a hint for you to know this.

Anyway, just a tip to save you some time....

Fletcher
May 16, 2013 at 11:35 PM
Good post. I will add this to the quick tips if you don't mind.

We have actually pondered whether to expand on this whole concept a bit and make it more strongly typed. We like that our current approach is practically identical to ASP.NET MVC. But what you describe above is not an uncommon problem as a result of this.


Thanks,
Markus