I am reviewing some ASP.NET MVC code developed by a third party and have encountered the following markup in a View:
<div class="panel_body">
<% Html.RenderAction(((string[])Model)[0], "Customer"); %>
</div>
<!-- Some HTML omitted -->
<div class="wizard_body">
<% Html.RenderAction(((string[])Model)[0], "Journey"); %>
</div>
Can anyone tell me why such casting and array accessing would be needed on the model?
The view is not strongly-typed.
The two RenderActions are rendering two separate Views that are defined in two separate ASP.NET MVC projects.
Edit
This View is initiated using the following action method
public ActionResult Index()
{
return View(new []{"Search"});
}
Thus ((string[])Model)[0] will return "Search".
Because the view is not strongly-typed,
Modelis of type object. Obviously, they have actually assigned an array of strings to the Model variable and the name of the action to be rendered is the first element of the array. Thus, they cast the Model to a string array and extract the first element.EDIT: If this is, in fact, the case, then you could avoid the casting by making the view strongly typed to
string[]. They you could simply do the indexing. The code itself makes me a little suspicious of its quality. I would probably have put the action name into ViewData and referenced it by name or used a strongly typed view with a view-specific model so that I could refer to the action by a property name. Either way would be clearer than what you have. I suggest refactoring to a strongly typed view with a view-specific model unless that’s the only property being referred to in that way.EDIT 2: Based on your update I would change it to:
and
Why even pass it as part of the model if the value is fixed?