Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC User Controls + ViewData

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.

like image 761
Ayo Avatar asked Oct 01 '08 13:10

Ayo


People also ask

Can we use user control in MVC?

In ASP.Net you need to register one tagprefix and then use the user control by tagprefix registered but in ASP.Net MVC using simple Thml. RenderPartial we can use the user control.

What is the use of ViewData in MVC?

In MVC, when we want to transfer the data from the controller to view, we use ViewData. It is a dictionary type that stores the data internally. ViewData contains key-value pairs which means each key must be a string in a dictionary. The only limitation of ViewData is, it can transfer data from controller to view.

What is ViewData in MVC with example?

ViewData is a dictionary of objects that are stored and retrieved using strings as keys. It is used to transfer data from Controller to View. Since ViewData is a dictionary, it contains key-value pairs where each key must be a string. ViewData only transfers data from controller to view, not vice-versa.

What is difference between TempData and ViewData?

To summarize, ViewBag and ViewData are used to pass the data from Controller action to View and TempData is used to pass the data from action to another action or one Controller to another Controller.


1 Answers

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

like image 117
Ricky Avatar answered Oct 20 '22 23:10

Ricky