views:

127

answers:

2

I am new to MVC. I am developing an web application in asp.net MVC. I have a form through which users can get registered, after registration user redirected to ProfileView.aspx page. till here everything is fine.

Now I want to show the articles headings posted by that user right under his profile. Right now I m using following code:

public ActionResult ProfileView(int id)
{

Profile profile = profileRepository.GetProfileByID(id);

var articles = articlesRepository.FindArticlesByUserID(id).ToList();

return View("ProfileView", profile);

}

Thanks for helping in advance
Baljit Grewal

A: 

you will want your page to inherit from ViewPage and then you can use your model inside the .aspx markup like <% foreach (var dinner in this.Model.Dinners) { %> <%= this.Html.Encode(dinner.Description) %> <% } %>

sorry eazel, i modified last line of my code, now please suggest me how can i pass articles to my view page.return View("ProfileView", profile);thanks for ur answer
Grewal
+1  A: 

I can think of two options:

Use the ViewData dictionary to store the articles.

public ActionResult ProfileView(int id)
{

Profile profile = profileRepository.GetProfileByID(id);  
var articles = articlesRepository.FindArticlesByUserID(id).ToList();
ViewData["Articles"] = articles;
return View("ProfileView", profile);
}

Or, if you want to avoid using ViewData, create a ViewModel. A ViewModel is kind of a data transport object. You could create a ProfileViewModel class like this:

public class ProfileViewModel
{
     public Profile Profile{ get; set; }
     public IList<Article> Articles { get; set; }
}

or just include the Profile properties you are using in your view, this will make binding easier but you'll have to copy values from your Model to your ViewModel in your controller.:

public class ProfileViewModel
{
     public int Id{ get; set; }
     public string Name { get; set; }
     .......
     public IList<Article> Articles { get; set; }
}

If you go for this last option take a look at AutoMapper (an object to object mapper).

Ariel Popovsky
hi Ariel Popovsky,thanks for ur reply, as i m new to MVC can you please suggess me soe urls from where i can learn MVC in detail?Thanks Again
Grewal
Sure, I recommend you start with Sharp Architecture, a framework based on ASP.net MVC. It includes a sample app and code generation templates.http://code.google.com/p/sharp-architecture/Billy McCafferty wrote a cool tutorialhttp://sharp-architecture.googlecode.com/svn/trunk/docs/Sharp_Architecture_Reference_Guide.docKazi Manzur Rashid has a great blog with cool articles on MVC, take a look at this:http://weblogs.asp.net/rashid/archive/2009/04/01/asp-net-mvc-best-practices-part-1.aspxAnd finally my own blog: http://geekswithblogs.net/apopovsky/Default.aspx I'll try to post something cool.
Ariel Popovsky
thanks Ariel Popovsky
Grewal