I am trying to make a combination of a html helper and a Reder CSHTML.
In other words, how can I use a cshtml "template" with a html helper so I do not need to parse all the ugly HTML in the methods.
I am currently using #Html.Action but that is not preferable as it needs a working URL.
#Html.RenderAction("GetThreads", "Forum")
public ActionResult GetThreads()
{
return new EmptyResult();
}
This gives the exception:
Argument 1: cannot convert from 'void' to 'System.Web.WebPages.HelperResult
Do I always need to add a route in the Global.asax file? Or are there ways to call Actions without it (like HTML helpers, but with a template file).
I have been trying with RenderPartial, but I keep getting these errors: Argument 1: cannot convert from 'void' to 'System.Web.WebPages.HelperResult'
Surround it with #{}
#{Html.RenderPartial("_SomePartial");}
Use #Url.Action to get the URL of an action. I think that is what you need.
Or #Html.RenderPartial or #Html.RenderAction will spit out the view to the page.
EmptyResult won't render a view. That might be your problem.
You might be looking for RenderPartial.
Related
I'm trying to better understand the "behind the scenes" on view components. I know that the standard approach is to invoke the ViewComponent by using
#await Component.InvokeAsync("MyViewComponent", itsModel)
or a view component tag helper in the .cshtml file.
It's possible however to use view componets other ways. So for example you can return a view directly from a controller by using return ViewComponent("MyViewComponent", itsModel); from an action method.
It seems like it should be possible to easily render the view component to a string. I see that on the ViewComponetResult returned by ViewComponent("MyViewComponent", itsModel); there is an ExecuteResult method.
In a controller it can be called like this:
ViewComponent("MyViewComponent", itsModel).ExecuteResult(this.ControllerContext);
At first blush I'd expect such a method to return a string of html. But it returns void. Why? What is ViewComponentResult.ExecuteResult doing if it's not returning a result? And how can I render a ViewComponentResult into a string of HTML?
I finally understand how this works. #TavisJ's comment is what tipped me of and sent me researching in the right direction.
On a ViewComponentResult or more universally, on any ActionResult the ExecuteResult returns void because the output of the ViewComponent is written to the associated HttpResponse.Body. This is true of the async version of the call as well except that of course it returns a Task.
So for example, if a person were to make the following call:
await ViewComponent("MyViewComponent", itsModel).ExecuteResultAsync(this.ControllerContext);
to capture the "output" of the view component, one would first need to replace the this.ControllerContext.HttpResponse.Response.Body with a stream of their choice before making the ExecuteResultAsync call so that the output is captured in a stream that can then be converted to a string.
Then of course, the original stream should be put back on the this.ControllerContext.HttpResponse.Response.Body so the rest of the view renders correctly.
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.
I have tried to follow this post in order to create a 3 page form wizard that passes data to each page.
He uses the HTML helper serialize, to serialize an object in the view.
#Html.Serialize("wizard", Model)
However this HTML helper isn't available in MVC 5 it seems.
I found another related post to this here where he suggests using the following to serialize the object.
#Html.Hidden("otherComplexData", new Microsoft.Web.Mvc.MvcSerializer().Serialize(complexObject))
But I then get the following error
There is no argument given that corresponds to the required formal parameter 'mode' of 'MvcSerializer.Serialize(object, SerializationMode)'
It seems to want a SerializationMode, however the documented one doesn't. https://msdn.microsoft.com/en-us/library/microsoft.web.mvc.mvcserializer.serialize(v=vs.118).aspx
What direction can I go in now?
Thanks.
Here's the Serialization option you need:
https://github.com/ASP-NET-MVC/ASP.NET-Mvc-3/blob/master/mvc3/src/MvcFutures/Mvc/SerializationMode.cs
Options are Signed or EncryptedAndSigned.
You can try that and see if it will work.
There's multiple ways to encode data that will work for you. You could put the values in a hidden input using Json.Encode for the view, and Json.Decode on the server side.
I'm working in an Asp.Net MVC 3 project where in one of my javascript file, a message is displayed to the user. This message in currently in one language but I want this message to be in multiples languages. I don't want to store theses languages directly in the javascript file but instead in a database. I want to know if it possible to transfer data (string) from the view to the javascript file.
One solution that I've found is to set data from my view in hidden fields and then extract it from my javascript file. The problem with this solution is that I must put a lot of hidden fields and this is very hard to maintain.
Is there any other solution to this problem?
You can try JavaScriptModel ( http://jsm.codeplex.com ).
With this library you can add variables by calling the following function in your controller action:
this.AddJavaScriptVariable("VariableNameInJavaScript", SomeValue);
You can pass primitive types or complex types.
Furthermore you could execute javascript functions with the following code in your controller action:
this.AddJavaScriptFunction("FunctionInJavaScript", SomeParameter, AnotherParameter);
Yes, your on the right path. All you need to do is hydrate your model appropriately and use Razor syntax to pass the value from the server to the client "along the following lines":
<script src="#Url.Content("~/scripts/yourapi.js")"></script>
<script>
yourapi.foo('#Model.SomeValue');
</script>
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.