5

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

2 Answers 2

6

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.

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

1 Comment

Thank you Matthew. Where I can found more document about this solution or a more complete source?
1

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?

1 Comment

Thank you. Is that the only way to do it? It seems so unintelligible.

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.