asp.net mvc - ascx control or what? - asp.net-mvc

so I have a form using typical Ajax.BeginForm inside a typical partial view (say T.ascx).
now I am adding validtion errors to Model using
catch (RulesException ex)
{
ex.AddModelStateErrors(ModelState, "Upload");
return PartialView("T.ascx");
}
this was working fine and the user control was clearly reflecting what was causing the validation error.
Now, I want it to be a part of the page. Now, you would tell me that go ahead and add a page and reference this control as RenderAction or RenderPartial, but the page would not have anything else apart from this, so it feels odd for me to want to add an aspx page just that I can use a control. I am sure there is a better way.
EDIT: It feels odd because the only purpose page would serve is reference the user control. Is there a better way? Like to serve ascx as a page itself (but using the master page).

Your partial view won't be referencing your master page file, even if it was possible to render it without any regular view.
I don't see a problem with having a view that just renders a partial view.

Related

ASP.NET MVC2 Master Page PlaceHolder visibility

asp:PlaceHolders have a visible property, and this controls whether their content is rendered on the page.
I have declared a PlaceHolder in an MVC2 Master Page and set it's visibility to false.
Please can you tell me how I can control the visibility of a PlaceHolder from within an MVC2 view template that inherits from an MVC2 master page?
This seems like a simple task, but I am struggling to see how it can be achieved. I think I should be able to get access to the PlaceHolder from within the descending view template (as with web forms code-behind) and just set it's visibility there but the way to do this is escaping me...
Don't do this. Placeholders are legacy from classic WebForms. Manipulating server side controls in an ASP.NET MVC application is very bad and you should never do it. So simply forget about setting properties on user controls. Fortunately in Razor placeholders have been completely removed and replaced by sections. So don't write code that you won't be able to migrate later.
One way to show/hide sections of your code in an ASP.NET MVC application is to use an if statement in your views. For example:
<% if (Model.ShouldShowSection) { %>
<div>Some super section</div>
<% } %>
In this example we are testing a boolean value on the view model which the controller action that rendered this view would set.

How to Load Content into an ASP.NET MVC Partial View in a Layout View

'm using ASP.NET MVC 3 with Razor views. I have a partial view which I would like to render on all pages so I am placing it in the site's main Layout page. However, I am not sure the best way to load data into the partial view. I could load it for each ActionMethod but there is there a way to do this globally across the entire app?
Move all the data loading logic for your partial into a separate action method.
Then in your layout page, instead of rendering the partial with a call to RenderPartial(), call the RenderAction() method.
RenderAction() makes a "child" action call - thus putting all the logic needed for that partial into one place.
Write action for this partial view in MasterController because every controller inherits from it, and place your partial view in shared folder and call it on Site's master page (like every site has a user control which gives login box until user is logged in else it displays logged in user info) ... hope it answer your ques ...

ASP.NET MVC 2 Submitting from partials in different views

If I have a partial that is used in multiple views that submits to its own dedicated action, then how do I know which view it has come from so I can go back to that view?
I think it's answered here: Retrieve the current view name in ASP.NET MVC?
Just to clarify - How do you mean 'so you can go back to that view'
If you use ajax forms ie Ajax.BeginForm for each of those partial views, they will all automatically handle their own psots to the url (hence controller) and can live peacefully in the parent view no matter which view it is.
if model validation happens, then you will see it in those partial views and modelstate will repopulate the posted values into the model for the user to 'fix'

Where to apply logic for a sidebar control in ASP.NET MVC

Take the example of wanting to have a "Latest news items" sidebar on every page of your ASP.NET MVC web site. I have a NewsItemController which is fine for pages dedicating their attention to NewsItems. What about having a news sidebar appear on the HomeController for the home page though? Or any other controller for that matter?
My first instinct is to put the logic for selecting top 5 NewsItems in a user control which is then called in the Master Page. That way every page gets a news sidebar without having to contaminate any of the other controllers with NewsItem logic. This then means putting logic in what I understood to be the presentation layer which would normally go in a Controller.
I can think of about half a dozen different ways to approach it but none of them seem 'right' in terms of separation of concerns and other related buzz-words.
I think you should consider putting it in your master page. Your controller can gather data (asynchronously, of course), store it in a nice ViewModel property for your view (or in TempData) and then you can call RenderPartial() in your master page to render the data.
The keeps everything "separate"
http://eduncan911.com/blog/html-renderaction-for-asp-net-mvc-1-0.aspx
This seems to address the question - even using the instance of a sidebar - but using a feature not included with MVC 1 by default.
http://blogs.intesoft.net/post/2009/02/renderaction-versus-renderpartial-aspnet-mvc.aspx
This also indicates the answer lies in RenderAction.
For anyone else interested, here's how I ended up doing it. Note you'll need to the MVC Futures assembly for RenderAction.
Basically you'd have something like this in your controller:
public class PostController
{
//...
public ActionResult SidebarBox()
{
// I use a repository pattern to get records
// Just replace it with whatever you use
return View(repoArticles.GetAllArticles().Take(5).ToList());
}
//...
}
Then create a partial view for SidebarBox with the content you want displayed, and in your Master Page (or wherever you want to display it) you'd use:
<% Html.RenderAction<PostController>(c => c.SidebarBox()); %>
Not so hard after all.
You can create a user control (.ascx) and then call RenderPartial().
Design a method in your controller with JsonResult as return type. Use it along with jQuery.
Use RenderAction() as suggested by elsewhere.
News section with ASP.NET MVC

JavaScript/jQuery insert ASP.NET MVC ViewUserControl into form

I have existing ASP.NET MVC View pages and View user controls which I currently use in normal straightforward ASP.NET MVC fashion, sometimes I use RenderPartialView or RenderAction, etc.
By themselves they include tag. I would like to dynamically load either Views or ViewUserControl based on the selection in a dropdown list.
I'm having trouble deciding should I remove from Views and controls and put it just into the one View that will do dynamic rendering or to leave it there and leave outside of the .
What do you think and how would you go about it?
I would probably try to load the contents of a div after doing an AJAX call to get the contents. See the AJAX get call in the jQuery docs.
Or are the possibilities of what control to load so small you could just hide/show div's that are already in the page?
You can use JQuery to get the HTML from your Partial views and substitute it in the div. It could be something like this:
$.get('/Controller/Action',function(data){
$('div').innerHtml(data);
});
I did it this way and it works. /Controller/Action can be a partial view which returns HTML.

Resources