View Composition

24 views
Skip to first unread message

Matt S.

unread,
Mar 26, 2013, 5:53:31 PM3/26/13
to fubumv...@googlegroups.com
I've been away from Fubu for a while now and I'm struggling with a simple concept, so I'm chalking it up to brain drain or something today.

Suppose I have a GET request that returns a view model with some summary information to display to the user. On the same view, I'd like to present a form for the user to fill out that will POST to another action. I can't for the life of me remember/figure out how to setup the view to do what I want. Here's my starting point:

My endpoint:
public class SomeEndpoint {
  public SomeViewModel Get(SomeRequestModel request) {
    // Get the summary information to display with the form.
    SomeViewModel model = GetSummary();
    return model;
  }

  public AjaxContinuation Post(SomeInputModel input) {
    // Process the input and indicate success/failure.
    return AjaxContinuation.Successful();
  }

  public class SomeRequestModel {}

  public class SomeViewModel {
    public Money OrderTotal { get; set; }
  }

  public class SomeInputModel {
    public string Name { get; set; }
    public int HowMany { get; set; }
    ...
  }
}

My view:
<viewdata model="Full.Namespace.To.SomeViewModel" />
<h2>Summary</h2>
<p>Order Total: <Display property="OrderTotal" /></p>
!{ this.FormFor[[Full.Namespace.To.SomeInputModel]]() }
  !{ this.Edit(???, m => m.Name) }
  !{ this.Edit(???, m => m.HowMany) }
!{ this.EndForm }


I know that there are DisplayFor and InputFor extension methods that accept another model than the view's model. I tried breaking the form up into a partial, but just could not figure out how to scope the Edit calls to the form's model. Do I need to setup a partial action that simply returns a blank SomeInputModel so my partial works the way I expect it? Can I do this type of composition with action-less views that work on a simple newed-up model?

This seems silly that I don't get it. Perhaps it's time to switch to a career in teaching. :-P


Thanks,

-Matt

Peter Wetzel

unread,
Mar 26, 2013, 7:04:23 PM3/26/13
to fubumv...@googlegroups.com
What's your diagnostics telling you? Definitely review routes page to see if there's something obvious there.

I normally use the controller "style" (with IgnoreControllerNamesEntirely), so my method names are more explicit:

public SomeViewModel Get_Something(SomeRequestModel request)

public AjaxContinuation Post_Something(SomeInputModel input)

I'm not saying your method signature is at fault, merely commenting on how mine usually is in the 1% chance it might be causing you grief.

Matt S.

unread,
Mar 27, 2013, 11:54:08 AM3/27/13
to fubumv...@googlegroups.com
Sorry for not being clear yesterday, Peter; and thanks for the tips.

So, I was struggling with how to get the calls to the Edit extension method scoped to SimeInputModel instead of SomeViewModel. Since the view is using SomeViewModel, I was trying to make the form use a different model for the inputs/labels, by way of the Edit method. I even tried something like this:

public class SomeViewModel {
  public Money OrderTotal { get; set; }
  public SomeInputModel Form { get; set; }
}
...
!{ this.Edit(m => m.Form.Name) }


That worked, but my labels had the "Form " prefix in them.

I decided yesterday was not a good day to be a coder, so I resigned to things much less taxing on my brain (this wasn't the only simple coding problem I ran into yesterday, mind you). Before I even got to a computer this morning, I had this "problem" worked out. I even grimaced at how stupid I was being yesterday, but it happens. Sometimes, the coding practices you try to avoid most are actually the correct way on rare occasions. In this case, it was simple inheritance.

public class SomeViewModel : SomeInputModel {
  public Money OrderTotal { get; set; }
}
...
!{ this.Edit(m => m.Name) }


Yeah, that easy. I think I'm still tainted by MS MVC too. At least as of version 3, you couldn't use view model inheritance and expect any results from attempting to display base properties. I don't know if that's changed in version 4, but I've been so used to ruling it out as option. Of course, this morning it was a "duh" moment that I remembered, "Fubu doesn't restrict me!"

Lesson relearned.


Thanks, Fubu!

Gary Cox

unread,
Mar 27, 2013, 11:58:43 AM3/27/13
to fubumv...@googlegroups.com
What I would do is have your form on a partial, then you can include your partial into your view using ${ this.Partial<Full.Namespace.To.SomeInputModel>() }


--
You received this message because you are subscribed to the Google Groups "FubuMVC Development Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fubumvc-deve...@googlegroups.com.
To post to this group, send email to fubumv...@googlegroups.com.
Visit this group at http://groups.google.com/group/fubumvc-devel?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Thank you,
Gary Cox

Matt S.

unread,
Mar 27, 2013, 12:10:08 PM3/27/13
to fubumv...@googlegroups.com
Gary,

Wouldn't that require a new GET action to return SomeInputModel?

Gary Cox

unread,
Mar 27, 2013, 12:12:12 PM3/27/13
to fubumv...@googlegroups.com
Yes, the get would return your SimpleModel
To unsubscribe from this group and stop receiving emails from it, send an email to fubumvc-deve...@googlegroups.com.

To post to this group, send email to fubumv...@googlegroups.com.
Visit this group at http://groups.google.com/group/fubumvc-devel?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Thank you,
Gary Cox

--
You received this message because you are subscribed to the Google Groups "FubuMVC Development Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fubumvc-deve...@googlegroups.com.
To post to this group, send email to fubumv...@googlegroups.com.
Visit this group at http://groups.google.com/group/fubumvc-devel?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Gary Cox

unread,
Mar 27, 2013, 12:13:01 PM3/27/13
to fubumv...@googlegroups.com
Sorry meant your SomeInputModel

Matt S.

unread,
Mar 27, 2013, 1:16:34 PM3/27/13
to fubumv...@googlegroups.com
That makes sense. And I could use this same technique for loading on-demand AJAX forms/data for other needs.

Thanks!

To unsubscribe from this group and stop receiving emails from it, send an email to fubumvc-devel+unsubscribe@googlegroups.com.

To post to this group, send email to fubumv...@googlegroups.com.
Visit this group at http://groups.google.com/group/fubumvc-devel?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 


--
Thank you,
Gary Cox
Reply all
Reply to author
Forward
0 new messages