My partial view, called _myTestView.cshtml
<div style="border:1px solid red;">
TEST
</div>
I call it like that
<div>
#Html.RenderPartial("_myTestView");
</div>
I get the following run time error
Compiler Error Message: CS1502: The best overloaded method match for
'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)'
has some invalid arguments
What am I doing wrong?
You're not inside a code block, so you don't use .RenderPartial
Just use .Partial and get rid of the ;
<div>
#Html.Partial("_myTestView")
</div>
Put it inside a code block
#{Html.RenderPartial("_myTestView");}
related What is the difference (if any) between Html.Partial(view, model) and Html.RenderPartial(view,model) in MVC2?
Related
Feels like a dumb question but I do not get it. How can I do fast string concatenation in Angular 2 Dart templates?
I have a seperate html file for my component lets say my_component.html:
Works:
....
<div id="abc">
{{order.pickupPlace.email}}
</div>
...
Works:
....
<div id="abc">
{{ ((order.pickupPlace.state) ? order.pickupPlace.state+" ":'')}}
</div>
...
Does not work:
....
<div id="abc">
{{"<br/>"+order.pickupPlace.email}}
</div>
...
Does not work:
....
<div id="abc">
{{order.pickupPlace.name+" "+order.pickupPlace.email}}
</div>
...
Have tried to find an answer in the docs here (https://webdev.dartlang.org/angular/guide/template-syntax#!#expression-operators) but no luck.
Of course I could use *ngIf on every element which I output conditionally but is there a way for simple string concatenation?
The best way is to declare a getter inside your Component controller that does the concatenation for you, you will get dart syntax support and the html template will looks cleaner.
String get myConcatenation => "${order.pickupPlace.name}${order.pickupPlace.email}";
<div id="abc">
{{myConcatenation}}
</div>
The last two examples can be made to work easily:
<div id="abc">
<br/>{{order.pickupPlace.email}}
</div>
And:
<div id="abc">
{{order.pickupPlace.name}} {{order.pickupPlace.email}}
</div>
Angular handles this pretty well. If you need some more complicated logic you can move it to the Dart code (but you cannot use HTML there easily).
If you find creating lot of weird logic consider creating more smaller components that can handle this for you.
I am new to razor and I need help to pass the action name from controller to view using ViewBag. Following is my code:
public ActionResult Index(int id = 0)
{
ViewBag.MasterId = id;
ViewBag.ActionName = "ApplicationProcess";
return View();
}
From the above action method I am passing action name of that is being used in view that is render in result of this function.
<body>
<h2>Index</h2>
<div style="width: 1100px;">
<div style="float: left; width: 400px;">
#{Html.RenderAction("MasterRecordProcess", "Process");}
</div>
<div style="float: right">
#{Html.RenderAction("MasterRecordBasicInfo", "Process", new { id = ViewBag.MasterId });}
</div>
<div style="float:right;margin-top:50px;" class="task-div">
#{Html.RenderAction(#ViewBag.ActionName, "Process", null);}
</div>
</div>
</body>
Now in above view I want to use the ViewBag.ActionName passed from controller in Html.RenderAction but this is giving me error message.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1973: 'System.Web.Mvc.HtmlHelper<OAC.Areas.Personal.Models.MasterModel>' has no applicable method named 'RenderAction' 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.
Kindly help how can I render the action name passed from controller in viewBag object. Thank you.
As #Stephen mentioned you must first cast the view bag content:
#{
var actionName= (string)ViewBag.ActionName;
}
Then you can use it:
#{Html.RenderAction(actionName, "Process", null);}
The error itself says that "consider casting the dynamic arguments or calling the extension method without the extension method syntax."
C# does not support calling an extension method (Html.RenderAction()) when any of the arguments is of a dynamic type. You have to either call the extension method statically or cast the argument to a non-dynamic type.
In this case you can the ViewBag to a String and use
UPDATE:
More general question what is the way to make a higher-order composition of views? The same way you pass a delegate into a method.
ORIGINAL QUESTION:
I have a page view and a control as a partial view. From the page view I render the control using Html.Partal("MyControl", myControlModel). Now this control has some areas that I wish were customizable from the page view. So that if the control is rendered from a different page these areas are filled with different content. Basically what I am looking for is a way to inject a piece of HTML from the page view into a partial view. Can I do it in MVC? If so, how?
Example:
Page view:
<div class="page">
#Html.Partial("MyControl", myControlModel, #<text>My <b>custom</b> piece of HTML which is different for each page<text>)
</div>
My control view:
<div class="my-control">
<div class="common-part-for-all-pages">
#Model.Value
</div>
<div class="part-that-has-to-be-customized">
#* there must be a piece of HTML provided somehow from the page view *#
</div>
</div>
Expected result:
<div class="page>
<div class="my-control">
<div class="common-part-for-all-pages">
#Model.Value
</div>
<div class="part-that-has-to-be-customized">
My <b>custom</b> piece of HTML which is different for each page
</div>
</div>
</div>
Add new properties to the viewmodel of the partial: "TemplateName" and "TemplateModel". Then do
<div class="my-control">
<div class="common-part-for-all-pages">
#Model.Value
</div>
<div class="part that has to be customized">
#Html.Partial(Model.TemplateName, Model.TemplateModel)
</div>
</div>
Or you could just add a string property "Template" and do
<div class="my-control">
<div class="common-part-for-all-pages">
#Model.Value
</div>
<div class="part that has to be customized">
#Html.Raw(Model.Template)
</div>
</div>
Call it like this
#{
// just set the property
myControlModel.Template = "some html";
myControlModel.Template = Html.TextBox(/*...*/).ToString();
myControlModel.Template = Template("hello").ToString();
}
#Html.Partial("MyControl", myControlModel)
#helper Template(string text)
{
<span>#text</span>
}
ToString() isn't necessary if MvcHtmlString type is used.
You'd need to create controller actions for this, but you can use #Html.Action("MyAction", "MyController", myModelObject) and pass any parameters from the page to partial view in the myModelObject parameter. It can be a bit of overkill but if your control/partial view needs to do any special C# code then this way works pretty well.
Make a class PartialModel, give it two properties string Name and object Model, then use #Html.Partial(pm.Name, pm.Model) in your partial view.
If you want to put different HTML inside every time, the above won't work, so read on.
You can use something similar to Html.BeginForm:
#using (Html.BeginMyContainer())
{
<h3>Hi!</h3>
<p>This is some custom content.</p>
}
(This would be a BeginMyContainer extension method on the HtmlHelper class.)
Your Html.BeginMyContainer method should return a class inheriting from MvcForm, which is IDisposable. In the BeginMyContainer method you'll write whatever HTML comes before the custom content of your container, and in the Dispose method of the returned MvcForm you'll write whatever HTML comes after your custom content.
When Razor processes the code I have above, it will:
Run the BeginMyContainer method at the start of the using statement, writing whatever HTML comes before the custom content
Write the HTML custom content inside of the using statement
Call Dispose on the MvcForm at the end of the using statement, writing whatever HTML comes before the custom content
Related: rolling my own #Html.BeginfBrm()
I have a file in my view folder Users called UserViewControl.cshtml.
My code in the actual view (Users.cshtml) is:
#Html.RenderPartial("RegisterViewControl")
Error:The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments
I do not want to type the path fully like this as the whole view folders might move in the future:
#Html.RenderPartial("~/Views/Users/RegisterViewControl.cshtml")
Code in RegisterViewControl.cshtml:
#model SampleMVC.Web.ViewModels.RegisterModel
#using (Html.BeginForm("Register", "Auth", FormMethod.Post, new { Id = "ERForm" }))
{
#Html.TextBoxFor(model => model.Name)
#Html.TextBoxFor(model => model.Email)
#Html.PasswordFor(model => model.Password)
}
This is a form that will be submitted by ajax, but I want all the validation from the viewmodel.
It should be like this:
#{Html.RenderPartial("RegisterViewControl");}
And that's because the RenderPartial extension method doesn't return anything. It writes directly to the output. In aspx you use it like this:
<% Html.RenderPartial("RegisterViewControl"); %>
instead of:
<%= Html.RenderPartial("RegisterViewControl") %>
So the same rules apply for razor.
You could alternatively use
#Html.Partial("RegisterViewControl")
I had this issue as well and got this directly from Scott Guthrie's blog post:
using #Html.RenderPartial() from a Razor view doesnt work.
Rather than call Html.RenderPartial() use just
#Html.Partial("partialname")
That returns a string and will work.
Alternatively, if you really want to use the void return method you
can use this syntax:
#{Html.RenderPartial("partialName")}
But #Html.Partial() is the cleanest.
The link for this is: http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx
I'm trying to use Html.RenderPartial in acsx file
and I'm getting an error:
Compiler Error Message: CS1973: 'System.Web.Mvc.HtmlHelper'
has no applicable method named 'RenderPartial' 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
<a href="/projects/<%=project.Id %>">
<% Html.Label("fdf"); %>
<% Html.RenderPartial("ProjectName", Model.Id); %></a></li>
<%} %>
However I've import neccessary namespaces, so it won't be error at
<% Html.Label("fdf"); %>
Are there any methods to use Html.RenderPartial in ascx file?
The compiler cannot choose the correct method because your Model is dynamic. Change the call to:
<% Html.RenderPartial("ProjectName", (int)(Model.Id)); %>
Or any other datatype Id is.
In case anyone else made the same mistake I did:
#Model MyViewModel
This will treat your model as dynamic
#model MyViewModel
This is a correctly strongly typed view. Note the lack of capitalisation!
Note, that this is Razor, unlike the original question.
The only way i found to pass eg. an IEnumerable was to create a local variable and pass in this one.
For Example
#{
IEnumerable<Demo.Models.Friend> friends = Model.Friends;
Html.RenderPartial("_FriendsList", friends);
}
Html.RenderPartial("_FriendsList", (IEnumerable<Demo.Models.Friends>)(Model.Friends)); did not work!