0

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

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

2 Comments

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
Sure, I recommend you start with Sharp Architecture, a framework based on ASP.net MVC. It includes a sample app and code generation templates. code.google.com/p/sharp-architecture Billy McCafferty wrote a cool tutorial sharp-architecture.googlecode.com/svn/trunk/docs/… Kazi Manzur Rashid has a great blog with cool articles on MVC, take a look at this: weblogs.asp.net/rashid/archive/2009/04/01/… And finally my own blog: geekswithblogs.net/apopovsky/Default.aspx I'll try to post something cool.
0

you will want your page to inherit from ViewPage and then you can use your model inside the .aspx markup like

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.