Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Pass some parameter to MVC User Control that is not in ViewData

Tags:

asp.net-mvc

I want to pass some parameters to my MVC UserControl like ShowTitle(bool) and the ViewData.Model.Row . How I define my usercontrol and pass them to it? Tanx

like image 960
Ata Avatar asked Dec 19 '08 20:12

Ata


2 Answers

You can use the RenderAction HtmlHelper, found in the MVC futures dll available at codeplex. In your main page ...

You need an action method on the controller with the parameters. The action then creates a ViewData for the usercontrol. The usercontrol is returned as a view with

return View("usercontrolname", model);

The .ascx file then uses the model for just the user control. The resulting HTML is rendered into the calling page.

like image 80
Matthew Avatar answered Sep 22 '22 18:09

Matthew


You can define your control as

public partial class MyUserControl : System.Web.Mvc.ViewUserControl<MyUserControlViewData> {
}

public class MyUserControlViewData {
    public IList<MyData> MyData { get; set; }
    public string SomethingElse { get; set; }
}

After that you can create an instance of MyUserControlViewData classin your controller, populate with data and pass it to the view. Is that what you're looking for?

like image 1
liggett78 Avatar answered Sep 20 '22 18:09

liggett78