In my layout file, I am displaying some text, and I want to hide this text only in one view. How do I tell what view is currently loaded from inside the view?
An easy way is to add some conditional logic to render in your _Layout as long as a viewbag bit is not set. If you don't want to render simply say so by defining the viewbag variable in the controller action and the layout won't render it.
There are more elegant solution involving attributes but this should get you going.
Don't forget the layout shouldn't know about specifically what view you're rendering thats a bleeding concerns. Viewbag helps by providing a communication and allowing decoupling of these two pieces.
You can set Id to that text container and in that specific view you can hide that Id by jquery
write this code in that view where you can to hide the text
$(function() { $("#id").hide(); }
Related
The layout page that calls the #Renderbody() method has a navigation bar, and I want to add an orange border to the button that represents the page the user is currently on. The only way I can think to do this is to add a class (.current or something) to the button on each individual page, but since the elements live in the _layout page, I can't change them per-page. This is easy to do with JavaScript, but is there a way to do it using strictly HTML and CSS?
Just because they creating the menu happens in _Layout doesn't mean that it cannot change on a page by page basis. You still have access to all the usual pipeline components whilst rendering _Layout. I can think of two methods that you could use, not sure which I prefer or if I even really like either of them, but they should show you that things are possible and perhaps you can think of a cleaner way to achieve it or someone else can think of a better way of doing it.
You could load something into the ViewData object in you action (or as part of a base controller or some generic code that is run for every action) that can then be fished out in the _Layout and used to add the class to the relevant button.
You could interrogate the Route object from within _Layout to figure out what page you where currently looking at and use that to add the class to the relevant button.
Hope this helps.
Cheers Mike
I have a menu and I used a cookie in order to set the menu as selected by refreshing. The menu is common to all classes, so I put it in the _layout.cshtml, but the same time I want a controller for this layout because there some function to set the menu as highlight. Can I create a controller for this?
Yes, you can call #Html.Action or #Hmtl.RenderAction to invoke a controller from the view. But this borders on mixing of concerns, as now your view is actively calling your controller.
The better approach is often to pass the appropriate data to the view as part of your view model.
Somewhat related answer of mine
RenderAction method documentation
But the same time I want a controller for this layout because there
some function to set the menu as highlight.
If you are doing something basic like highlighting the current page, there may be a simpler solution. You could put the current page ID into the ViewBag and retrieve that value in your main _Layout file and use it to select the appropriate item.
Partial actions may be what you looking for here - this article may be some help. It relates to MVC2 but the principles remain the same.
create an action for menu, and put menu ui in the view, and then call Html.Action("menu action name","controllern name") on layout page.
I have a user control which contains a number of child controls. I want to be able to change the layout of the control (i.e. change the markup surrounding the child controls) via a property of the user control, but I don't want to have to duplicate the child controls themselves. This precludes using, say, a MultiView with different versions of the layout in each view, since in that case I'd have to duplicate the controls. I'd also rather use markup than emit the code for the control directly (as you'd do in a server control).
To give a contrived example, say I have a label and a textbox. In one layout, I want the label and textbox to be contained by divs and laid out on top of each other. In an alternate layout, I want them to be contained side by side in a table.
Should I just go ahead and duplicate the controls between each view? It seems like a violation of the DRY principle, but I'm not sure if I have much choice. A partial solution would be making the views of a multiview into naming containers so that I could at least save myself the time of having to prefix the duplicate control IDs with something to make them unique.
Thanks! Please feel free to ask if the above doesn't make sense.
You could use a templated user control to define header and footer templates for each section of the user control. From there you can use CSS and/or public properties on the user control to style the pre-defined (non-templated) components of the control.
It really sounds like maybe you should go ahead and create a server control instead of just a user control. You'd have to create the markup in code, but it would allow you the best flexibility.
An idea is to create a second user control, that is clone with the first on the design file but have the same code file.
FileLayout1.ascx
FileLayout2.ascx
both of this have
CodeFile="FileLayout1.ascs.cs" Inherits="FileLayout1"
FileLayout1.ascs.cs
You can load the 1 or the 2.
The limitations/problems are that both ascx must have the exactly same control names, and you have the same control 2 times (but the code is only one time)
From my point of view the good think is that you free to design the layout as you like on both way.
A friend has asked me to help him with a project that's MVC. This is my first experience with MVC. I'm trying to build the MVC components for a form for him.
A page has a modal popup which uses a JavaScript to POST or GET and receive HTML backāthat it displays inside the popup modal.
I need to create an MVC form that has validation to display inside aforementioned popup. This popped-up form will be used elsewhere on the site, so needs to be modular.
Should I create an ActionResult in my Route's Controller that returns an View (.aspx) containing my form? Should I instead create a Partial View (.ascx) that has the form, then use that Partial View in a View for my Route so other parts of the site--other Routes--can do the same?
I'm stuck at that basic understanding. From there, I don't even know what to do about the validation (was told the same validation will be used on a nearly identical form) and how a ViewModel fits into this like is used elsewhere on the site.
I've been reading a lot and looking at a lot of examples but I'm still confused.
You've got two separate questions here really. To deal with whether you should use a partial view or a regular view, just think of it this way. Does the HTML content in question represent a full page, or just a piece of a page that will be reused inside of other pages?
If it's the former, then use a full View. If it's the latter, then put it in a partial view. It sounds to me like you just need a full View. In either case, it's easy to change to the other if it turns out it's not meeting your needs.
As to validation, take a look at xVal if you're using ASP.Net MVC 1. It allows for easy validation using attributes on your models.
When you make your post from the modal popup do the validation then in the actionmethod you created specifically for that popup. If you want client side validation write up the js to do it.
As for the modal markup and what not just create a partial view for that, shouldn't be a big deal.
I'm writing an ASP.NET app in which a table of objects is created by the user client-side.
I envisage them clicking "Add item" and a new 'row' is created in the table with textboxes for them to enter their stuff.
I can do this fine by adding HTML with jQuery. The problem is that one of the fields in the row for the user to fill in needs to be a colour picker.
I have an ASP.NET web user control for my colour picker. How do I add an instance of it to the page within my html row? Or am I barking up the wrong tree here - is there a better way of encapsulating the functionality of my colour picker so that it can be put on every row?
No, you can't add a server-side asp.net control to a page that has already been rendered using client-side techniques (aka Javascript)
Two options:
Firstly, switch to using a client-side colour picker. You can then have the data from this included in the post-back by dynamically adding hidden fields to your form.
Secondly, have a single editing panel which includes your colour picker. Users then select a row to edit, which updates the edit panel with current values etc. Values are stored in hidden fields created when you dynamicaly add rows to your table, and included in the post-back
Without seeing your UI, I can't comment as to which would be best. The asp.net control might look nicer, but it might be difficult to work into your design. A pure client-side solution might fit your designer better, but might not look so good. You also need to consider what happens if / when a users adds lots of rows (this might be 10, 50 or 100 depending on your app /code). Lots of dynamically added controls (the first solution) might cripple the performance of the page.
I'm not sure what version of ASP.NET you're using, one approach that would work is to turn your usercontrol into a custom control. You'd then need to implement ICallbackEventHandler (the first way to do Ajax on asp.net); for sure it's a bit more work but it does give you a good level of control.
Alternatively, you could try this
You can't add ASP.NET controls with jQuery (at least not easily). You could, however, perform a postback when you need to add the colour picker to the row.
In the code in front declaratively define a template of what the new row should look like, then hide it using css.
When the user clicks the 'Add new button' select and cloen the contents of your hidden template and write that into your target div. Just make sure to remove the hiding css when you do this.
You will, of course, just be copying the rednered html of your server controls, but htis apporach may give you a quick and easy way of doing what you need