Asp.net-mvc – MVC User Controls + ViewData

asp.net-mvcmodel-view-controlleruser-controlsviewdata

Hi im new to MVC and I've fished around with no luck on how to build MVC User Controls that have ViewData returned to them. I was hoping someone would post a step by step solution on how to approach this problem. If you could make your solution very detailed that would help out greatly.

Sorry for being so discrete with my question, I would just like to clarify that what Im ultimatly trying to do is pass an id to a controller actionresult method and wanting to render it to a user control directly from the controller itself. Im unsure on how to begin with this approach and wondering if this is even possible. It will essentially in my mind look like this

public ActionResult RTest(int id){
RTestDataContext db = new RTestDataContext();
var table = db.GetTable<tRTest>();
var record = table.SingleOrDefault(m=> m.id = id);

return View("RTest", record);
}

and in my User Control I would like to render the objects of that record and thats my issue.

Best Answer

If I understand your question, you are trying to pass ViewData into the user control. A user control is essentially a partial view, so you would do this:

<% Html.RenderPartial("someUserControl.ascx", viewData); %>

Now in your usercontrol, ViewData will be whatever you passed in...