Reputation: 9648
ASP.NET allows me to pass in view's name (as string) but in most cases I want to pass a string as an argument. What I do is cast string to object
then pass it to the View.
[ChildActionOnly]
public ActionResult MyView(string model)
{
return View((object)model);
}
Isn't there a way I do not need to cast it? It looks ugly for me.
Upvotes: 0
Views: 112
Reputation: 93464
You can do this, but i'm not sure it's any better:
return View(null, null, model);
Or you can do this:
return View("MyView", null, model);
Note: using the overload with two parameters won't work, because it will select the layout overload instead of the model version instead.
Another option would be to simply use a string array instead, which doesn't take much more overhead.
public ActionResult MyView(string[] model)
{
return View(model);
}
But, the fact is, you ARE in fact casting to an object, even when you don't specify it for other types... it's just implicit.
using the cast is just being explicit, and there's really nothing wrong with that.
Upvotes: 2
Reputation: 3996
That's an MVC convention. Typically, any string passed to the View is intepreted as a View Name to render. The only way to pass a string is cast it to an object as you have done.
If you don't like that, you can always pass it in addition to the view name.
public ActionResult MyView(string model)
{
return View("MyView", null, model);
}
Upvotes: 0