0

I have a View that invokes a few child controller actions. One of these child actions will check for an optional query-string value, this particular query-string will be an integer and I'm wondering if it's prudent to have this value as a parameter in the child action?

The reason I'm not using a parameter is because the View would then have to parse the query string as an integer and also perform null checks, hence why the child action just checks for the presence of the query string internally.

Is this the recommended approach? Or is it better (and more testable) to have an argument in the action method?

Thanks

1 Answer 1

1

You would simply use Html.Action / Html.RenderAction and pass the data you need specifically to this action method:

 public ActionResult Sample(SomeViewModel model)
 {
     // Do Stuff
 }

And then in your view:

 @Html.Action("Sample", new { model =  new SomeViewModel { Property = "HelloWorld" } });
Sign up to request clarification or add additional context in comments.

3 Comments

I'm aware of how to implement it in this way, but I was more interested in what the recommended approach was?
That would be the recommended approach. You don't have to use a view model (it can really be any type), but you would want to pass the arguments explicitly from your parent model.
Thanks, that what I essentially wanted to know, still trying to snap out of the Web-Forms way of doing things :-)

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.