Use a variable to reference a partial view's location - c#

I am new to asp.net core and am trying to render a partial view in an ASP.Net Core application. The address of the partial view is determined at run time. I have constructed a view model which is parsed into the view from the controller and contains the desired file address.
The following code throws the following compiler error:
#Html.Partial(Model.File);
Error CS1973 'IHtmlHelper' has no applicable method named 'Partial'
but appears to have an extension method by that name. Extension methods
cannot be dynamically dispatched. Consider casting the dynamic arguments or
calling the extension method without the extension method syntax.
Any help would be greatly appreciated.

The solution in the end was to specify the type in the Razor file. The following line worked:
#Html.Partial((string) Model.File)

Just to add to Lachlan Fergusson's excellent answer (thank you!) just to say that you also get this message if the name of your view contains a variable, without it's type.
So, the following line threw the error for me:
#Html.Partial("UserDetailsPartial." + language, Model)
...but it went away when I added this...
#Html.Partial("UserDetailsPartial." + (string)language, Model)
Behind the scenes, I had different partials based on language,
UserDetailsPartial.es.html
UserDetailsPartial.de.html
UserDetailsPartial.fr.html
The strange thing is that previously (with an earlier version of .Net Core?) the original line worked fine.
So, add this to Microsoft's "list of error messages which don't really explain what the problem is.."

Related

Two Grid Packages Installed: The call is ambiguous between the following methods or properties

I inherited a rather old MVC project that had a Grid package installed, Grid.MVC. It is used extensively, and taking it out or replacing it is not an option (client won't pay for it.)
We just built out a new portal section to the site, and in it, we used a new (and better) grid, NonFactors.Grid.Core.MVC5. Much more features and options.
But here's the problem. In all the places where the old grid is used, I now get this run-time error:
The call is ambiguous between the following methods or properties: 'NonFactors.Mvc.Grid.MvcGridExtensions.Grid(System.Web.Mvc.HtmlHelper, System.Collections.Generic.IEnumerable)' and 'GridMvc.Html.GridExtensions.Grid(System.Web.Mvc.HtmlHelper, System.Collections.Generic.IEnumerable)'
This should be a simple fix. All I need to do is tell the old grids which one they are. But i'm not figuring out the syntax. In the view's, they all have a #using which points to the correct (old) version.
#using GridMvc.Html
....
#Html.Grid(Model.Leads).Named("userGrid").Selectable(false).Columns(c =>
{
....
}
A pic might be more helpful:
I've tried various means of a full path in the view, but the syntax is never right...
#GridMvc.Html.GridExtensions.Grid(Model.Leads).... // Nope
#Html.GridMvc.Html.GridExtensions.Grid(Model.Leads).... // Nope
etc etc etc.
Again, this is probably simple. I'm just not getting it.
Try passing the HtmlHelper instance as first argument to the extension method:
#NonFactors.Mvc.Grid.MvcGridExtensions.Grid(Html, Model.Leads)
#GridMvc.Html.GridExtensions.Grid(Html, Model.Leads)
Extension methods are callable as standard static methods, but just take the parent type as a first parameter, so you should be able to pass the HtmlHelper as so:
#GridMvc.Html.GridExtensions.Grid(Html, Model.Leads)
Disable one of the grid templates from the web.config
in the views folder.
-->

Syntax Error Passing Razor Markup As Parameter

I'm trying to write a custom control and associated html helper that can have it's output changed by providing a template. I've found examples of this on the web (example) but keep hitting upon a strange syntax error that I don't understand.
Here's a simplified version of what I've got so far:
Method signature that takes the razor template
public IHtmlString Template<T>(Func<T, object> template) {
// Implementation not important
}
Calling the method in my view:
#Html.Control(Of Integer)().Template(#<div>#item</div>)
However, I keep getting a syntax error which is showing on the closing bracket of the Template method. I can't figure out what I'm doing wrong here nor could I find any similar problems else where. Does anyone have any ideas what's going wrong here?
Also yes I realise that the helper is written in c# and the razor is in vb sadly that's the way things are round here.

ASP MVC has a strange interpreting of model - extension methods cannot be dynamically dispatched

Sorry if this is obvious, but it is very confusing for me. After specifying model type:
#model MyNamespace.MyModel
Which does not display any error and the path to the MyModel is correct, same is the model name, some of the methods seem not to recognize the Model type as follows:
#Html.Partial("_Title", Model)
Which outputs the following error:
extension methods cannot be dynamically dispatched
Which should not be shown, since the Model type is specified. Also if I am trying to cast it again:
#Html.Partial("_Title", (MyNamespace.MyModel)Model)
Resharper is saying that Cast is redundant, but the error goes away.
What could cause this behavior to an MVC view?
Note: I have other views which have the model defined in the very same way and which are using exactly same partial views, but they are working properly.
I have tried deleting the file and recreating and the errors keep coming back.
I'm not sure why you'd need to do that at all. When you call Partial() and don't specify the model, the current model is passed to the Partial :)
The reason this happens is because internally when you pass a model to Partial() MVC copies the current ViewData (including ViewData["Model"]) and passes it to the next partial, if you don't specify the value the current value is used.

Argument to Partial View is the Wrong Type--How is this Possible?

I have the following line in one of my views (cshtml files):
#Html.Partial("_ProviderRatingWidget", Model.RatingInfo)
In my lab, everything seems to be working fine. But in deployment, we are seeing the following error:
System.InvalidOperationException: The model item passed into the dictionary is of type 'ProviderContactInfo', but this dictionary requires a model item of type 'RatingCategorySummaryModel'.
Model is explicitly of type ProviderContactInfo, and Model.RatingInfo is explicitly of type RatingCategorySummaryModel. And so how can this line ever pass type ProviderContactInfo?
Can anyone see any possible circumstances where this line could produce that error?
EDIT
Oh, I should point out that the line above is also in a partial view. (The partial view is loading another partial view.) That might be related to the problem.
I have verified that the current model will be passed if the property you are trying to pass is null. You can get around the issue with this:
#Html.RenderPartial("_ProviderRatingWidget", new ViewDataDictionary(Model.RatingInfo));
Hope that helps!
mmm I not test, but maybe this work:
#Html.Partial("_ProviderRatingWidget", ((RatingCategorySummaryModel)Model.RatingInfo))
Regards.,
k

RenderPartial from different folder in RAZOR

I've been trying to convert my aspx pages to cshtml and having an issue with rendering partial pages from another folder.
What I used to do:
<% Html.RenderPartial("~/Views/Inquiry/InquiryList.ascx", Model.InquiryList.OrderBy("InquiryId", MvcContrib.Sorting.SortDirection.Descending));%>
I would think that the equivalent would be:
#Html.RenderPartial("~/Views/Inquiry/_InquiryList.cshtml", Model.InquiryList.OrderBy("InquiryId", MvcContrib.Sorting.SortDirection.Descending))
This is obviously not working, I am getting the following error.
CS1973:
'System.Web.Mvc.HtmlHelper'
has no applicable method named
'Partial' but appears to have an
extension method by that name.
Extension methods cannot be
dynamically dispatched. Consider
casting the dynamic arguments or
calling the extension method without
the extension method syntax.
How would I achieve this with using the Razor view engine?
The RenderPartial does not return a string or IHtmlString value. But does the rendering by calling Write in the Response.
You could use the Partial extension, this returns an MvcHtmlString
#Html.Partial( ....
or
#{ Html.RenderPartial(....); }
If you really want RenderPartial
The compiler cannot choose the correct method because your Model is dynamic. Change the call to:
#Html.RenderPartial("~/Views/Inquiry/_InquiryList.cshtml", (List<string>)Model.InquiryList)
Or to whatever data type InquiryList is.
Remember to include your strongly typed #model directive in your new Razor view. It is an easy step to miss when converting views from .aspx to .cshtml. If you forget, that 'System.Web.Mvc.HtmlHelper' has no applicable method named 'Partial' error message could appear.

Categories