How to render a controller's action result to string? - asp.net-mvc

according to the ASP.NET MVC Razor: How to render a Razor Partial View's HTML inside the controller action I'm can render a View to a string. But, is it possible to invoke an action inside the controller and render the result to a string ?

What I tend to do for things like this is return a PartialView which will have any of the HTML I need for the JavaScript to use. Keep in mind that in HTTP, everything is text already, so you don't need to do something special to render the result to a string to return to an AJAX call.
I'll have a PartialView which has the HTML I need, pass it back to the Ajax call using something like return PartialView("ViewName", model), then do whatever I need to on the client side - such as appending that HTML to a node in the DOM.

Related

ASP.NET MVC Hybrid of ContentResult and ActionResult

I am after creating something like a ContentResult, but have it render within the #RenderBody() tag of a masterpage. Is this possible? I can't seem to find a clear answer for it.
No; you can't do that.
Instead, you can make an empty Razor view that simply renders HTML from its model.
You can have an action method return a ContentResult, and then invoke it in the master page by doing:
#Html.Action("Action", "Controller")
I'm not sure what you mean by doing it in #RenderBody, but you can do it within the master page itself, or within each content page.
The content will be generated in the page; you could also use a partial view approach too.

Partial content accepting HTML as parameter in ASP.NET MVC 3

I have a concept of a modal window in my MVC application that renders some HTML that wraps content that will eventually show inside on the modal. The content will be HTML either directly hard coded in the view or generated by an Html helper as passed in as a parameter.
I'd like to wrap the modal content in a Razor template or partial in my application to avoid spreading it all over my application as it used in a number of pages. What's the easiest way of doing this? Can I achieve something similar in i partial view with out a model?
Build a viewmodel for the partial view... a simple C# class with a string property for the Html... using the [AllowHtml] attribute on it will let you stuff HTML in the object.
In your parent view, stuff your HTML into this view model and pass it to the partial when you call it.

Validation Summary For Ajax Call

What is the best way to send back Validation Summaries back to the client when making an ajax call using jquery?
An easy way to achieve this is to have a div inside your page containing a partial with the validation summary. When you invoke the controller action using AJAX it would return this partial and you will be able to update the summary. Example:
$('#validationSummary').load('/home/someaction');
where the action would return a view:
public ActionResult SomeAction()
{
return View();
}
and the corresponding view:
<%= Html.ValidationSummary() %>
This way the controller action directly sends the partial HTML that needs to be updated/substituted inside your page. If your controller action returns JSON you will need to accommodate in your existing JSON structure an additional parameter containing the list of error messages so that in the AJAX success callback you could update the corresponding DOM sections. A bit more work here but with things like jQuery Templates this might even be fun :-)
You should return them back in Json. You can do this by return Json("Your Message")
If you're using ASP.NET MVC 3, look into the new remote attribute: http://davidhayden.com/blog/dave/archive/2011/01/04/ASPNETMVC3RemoteValidationTutorial.aspx

Display AJAX controller action result using HtmlHelpers

I have a fairly complex object which has some C# code written to render it as HTML in various views.
There is also a view which can call an AJAX method of a controller, which returns the complex object serialized to JSON which should then be displayed.
This seems to leave me requriring complicated duplicate code to render the resulting JSON as HTML using Javascript/jQuery.
The obvious solution is to render the HTML in the controller action and return this from the AJAX call. However this seems in violation of the MVC pattern so not really a good option.
Is there a different way I can render the object returned from the AJAX method making use of the existing C# code?
Thanks.
Create a PartialView to which you render the object, and return that.
As mentioned, either create a PartialView user control and return that, which you can inject the HTML in nicely (returns the HTML as a string) or you can use a templating option in JQuery or something else to do the UI generation for you.
HTH.

How can I use Html.ValidationSummary with Ajax.BeginForm?

I have an AJAX form that I am creating in my MVC project. If the form is submitted using normal browser function and a page refresh occurs I get validation information rendered in the form (the built in MVC validation based on ViewData.ModelState).
Is there a similiar validation mechanism for AJAX forms?
<% using (Ajax.BeginForm("Create", "GraphAdministration", new AjaxOptions()
{
OnSuccess = "newGraphSuccess",
OnFailure = "newGraphFailure",
HttpMethod = "POST"
}))
{ %>
<!-- some form stuff in here !-->
<% } //end form %>
It really depends on where you are getting the content from to display once the form has been posted. The Validation summary is performed created on the server so that is where you have to do the work.
As an example I was using some partial content in an .ascx file to render a form. You get the form in the page the first time round by calling the action directly with Html.RenderAction
You would have your Ajax.BeginForm etc. in the .ascx file. Then call it directly in an action.
When the Ajax call is made from the browser you get it to post to the same action. That way you can do all of the server side validation that you would normally. You should set up the Ajax call to replace the original form with the new html that is returned by the action.
One thing that you have to be aware of is that the replace JavaScript will replace the content of an element not the element itself so remember to us the id of a surrounding element.
Apologies if that is a little convoluted, if you want more details just comment and I'll flesh out the relevant bits.
Extra Detail:
All of this assumes that you are doing all of the validation on the server.
You are going to have a View that has all of the page stuff in it and then some partial content in a .ascx file, this is where your ajax form lives, it needs to be set to replace content by id. Its easiest if it has the same name as the action your ajax is going to call.
You can use Html.RenderAction to get it into the View. You can also pass in data with other versions of the same method. Your essentially calling it in the same way your ajax code will.
You will need to wrap it all in a div with an id set. Use this id in the partial as the content to replace.
When you render the page the html for the form and all of the ajax stuff will get put in.
When the ajax action is called the partial content will be returned with any validation performed. It will replace the content of the div that you gave the id to.
You can have different versions of the action by using [AcceptVerbs(HttpVerbs.Get)] and [AcceptVerbs(HttpVerbs.Post)] attributes
The main problem with this method is that its not self contained, the div with the id is external to the partial.

Resources