Skip to main content
Tweeted twitter.com/StackTridion/status/734666543682998272
edited body; edited title
Link
Dominic Cronin
  • 18.5k
  • 3
  • 23
  • 74

How do I make use of DD4T's provided ViewModelssViewModels?

edited body; edited title
Source Link
Dominic Cronin
  • 18.5k
  • 3
  • 23
  • 74

How do I make use of DD4T's provided modelViewsViewModelss?

I have been able to create a modelViewviewModel class which maps the fields of my schema using DD4T's "ViewModels" attributes:

[ContentModel("Content", true)]
public class ArticleViewModel
{
    [TextField(FieldName = "title")]
    public string Title { get; set; }
    [DD4T.Mvc.ViewModels.Attributes.RichTextField]
    public MvcHtmlString Body { get; set; }
}

I would now like to make use of this in my view. I have an action method in my controller:

public ActionResult Component(ComponentPresentation componentPresentation)
{
    return base.ComponentPresentation();
}

This gives me a Model of type DD4T.ContentModel.ComponentPresentation, while obviously I would like it to be an ArticleViewModel. I need to do something in my controller to instantiate my ArticleViewModel and have it populated with the fields from the component presentation, but it's not clear to me what the correct way to do this is. Obviously, I could just "new one up" and populate the fields manually, but then what would be the point of the annotations?

Actually - I'm not at all sure that I'm doing the right thing here, so any clarity anyone can add would be useful.

How do I make use of DD4T's provided modelViews?

I have been able to create a modelView class which maps the fields of my schema using DD4T's "ViewModels" attributes:

[ContentModel("Content", true)]
public class ArticleViewModel
{
    [TextField(FieldName = "title")]
    public string Title { get; set; }
    [DD4T.Mvc.ViewModels.Attributes.RichTextField]
    public MvcHtmlString Body { get; set; }
}

I would now like to make use of this in my view. I have an action method in my controller:

public ActionResult Component(ComponentPresentation componentPresentation)
{
    return base.ComponentPresentation();
}

This gives me a Model of type DD4T.ContentModel.ComponentPresentation, while obviously I would like it to be an ArticleViewModel. I need to do something in my controller to instantiate my ArticleViewModel and have it populated with the fields from the component presentation, but it's not clear to me what the correct way to do this is. Obviously, I could just "new one up" and populate the fields manually, but then what would be the point of the annotations?

Actually - I'm not at all sure that I'm doing the right thing here, so any clarity anyone can add would be useful.

How do I make use of DD4T's provided ViewModelss?

I have been able to create a viewModel class which maps the fields of my schema using DD4T's "ViewModels" attributes:

[ContentModel("Content", true)]
public class ArticleViewModel
{
    [TextField(FieldName = "title")]
    public string Title { get; set; }
    [DD4T.Mvc.ViewModels.Attributes.RichTextField]
    public MvcHtmlString Body { get; set; }
}

I would now like to make use of this in my view. I have an action method in my controller:

public ActionResult Component(ComponentPresentation componentPresentation)
{
    return base.ComponentPresentation();
}

This gives me a Model of type DD4T.ContentModel.ComponentPresentation, while obviously I would like it to be an ArticleViewModel. I need to do something in my controller to instantiate my ArticleViewModel and have it populated with the fields from the component presentation, but it's not clear to me what the correct way to do this is. Obviously, I could just "new one up" and populate the fields manually, but then what would be the point of the annotations?

Actually - I'm not at all sure that I'm doing the right thing here, so any clarity anyone can add would be useful.

Source Link
Dominic Cronin
  • 18.5k
  • 3
  • 23
  • 74

How do I make use of DD4T's provided modelViews?

I have been able to create a modelView class which maps the fields of my schema using DD4T's "ViewModels" attributes:

[ContentModel("Content", true)]
public class ArticleViewModel
{
    [TextField(FieldName = "title")]
    public string Title { get; set; }
    [DD4T.Mvc.ViewModels.Attributes.RichTextField]
    public MvcHtmlString Body { get; set; }
}

I would now like to make use of this in my view. I have an action method in my controller:

public ActionResult Component(ComponentPresentation componentPresentation)
{
    return base.ComponentPresentation();
}

This gives me a Model of type DD4T.ContentModel.ComponentPresentation, while obviously I would like it to be an ArticleViewModel. I need to do something in my controller to instantiate my ArticleViewModel and have it populated with the fields from the component presentation, but it's not clear to me what the correct way to do this is. Obviously, I could just "new one up" and populate the fields manually, but then what would be the point of the annotations?

Actually - I'm not at all sure that I'm doing the right thing here, so any clarity anyone can add would be useful.