1

Hi I am still kinda new to asp.net and what it can do. I am looking to use a few strongly typed partial views on one main homepage. I am not sure how to do this with a repository and Irepository.

I have only done this up to now and keep getting an error when I try to load the page.

  <%Html.RenderPartial("~/Views/Shared/Partial.ascx", Model);%>

Would I have to use viewData to pass information to the view?? any help would be grateful, along with any examples. Can anybody please help regards

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Trev
  • 31
  • 1
  • 7

2 Answers2

2

I'm going to differ from the other answers and say: No. You should not use tempdata to pass information into a partial view. Depending on what you're doing, you should instead create a Composite View Model. Best practices state that you should not be passing Models direct to your Views and should instead create ViewModels.

See the accepted answer here about Composite View Models: ViewModel Best Practices

public class CompositeViewModelAB {
    public ViewModelA viewModelA { get; set; }
    public ViewModelB viewModelB { get; set; }
}

Then:

@Partial("~/path.cshtml", Model.viewModelA)

This satisfies your case as everything will be strongly typed. Your home page is strongly typed to the composite view model, and your partial will be strongly typed to the view model that's contained within the composite view model.

Community
  • 1
  • 1
Adam Terlson
  • 12,610
  • 4
  • 42
  • 63
1

Would I have to use viewData to pass information to the view?

Yes.

If you don't want to pass data through the same controller method to the partial views, consider using RenderAction instead of RenderPartial. You will need more controller methods, but you won't have to repeat your data binding code in your existing controller methods every time you use a partial view.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • Harvery - the error I am getting is : Object reference not set to an instance of an object. How would I use viewDate to actually pass it to the view? please could you give me an example if possible??including use of the repository/Irepository/controller – Trev Apr 11 '11 at 20:31