Menu State - ASP.Net MVC - asp.net-mvc-2

In my ASP.Net MVC View, I have a menu. The menu consists of a number of Parent Items, with Child Items (anchor tags) underneath.
I'm using JQuery to toggle the menu items opened and closed, as the user clicks on them.
Unfortunately, once the user clicks onto an anchor tag, and gets sent to another page, the menu state is lost, and the menus go back to being closed.
Now if this was "normal" ASP.Net I'd be sticking the menu state information into the ViewState, and storing the information that way.
Is it possible to do something similar in MVC?? This is the first MVC project that I've worked on, so go gentle with me!

So let me first make sure I understand you correctly.
The user will click on a menuitem, which will take him to another page. And when he gets to that page you want the menu to reflect the fact that he is on that page? Correct?
This doesn't sound too tricky. I assume the menu is a partial view - so you would render it something like this:
<div id="menu">
<% Html.RenderPartial("Menu"); %>
</div>
So the view will already know which menu item triggered it. For example, if the user clicks on Widgets->New, you might return the NewWidget.aspx view, and it will know that the Menu item to highlight is Widgets->New. So you simply use the overload for RenderPartial to specify the name or id of the menu item to highlight.
<div id="menu">
<% Html.RenderPartial("Menu", "newWidgetLink"); %>
</div>
If it is NOT the case that a view already knows which Menu Item to highlight, you will need to pass the id of the menu item with the link. So your link generation will look something like this:
Html.ActionLink("Menu Item Text",
"Controller name goes here",
"Action name goes here",
new { menuItem = "menuItemId goes here" },
null
)
Then your action will need to handle this parameter. Easiest would be to take the parameter and add it to the ViewData. The Menu will then check in the ViewData for the Id of the MenuItem to highlight.

Maybe this: ASP.NET MVC - Is there a way to simulate a ViewState?
http://blog.maartenballiauw.be/post/2009/10/08/Leveraging-ASPNET-MVC-2-futures-ViewState.aspx
http://forums.asp.net/t/1285163.aspx
and this might help too
http://webcache.googleusercontent.com/search?q=cache:y26JBxHjbBUJ:www.beansoftware.com/ASP.NET-Tutorials/Intro-ASP.NET-MVC.aspx+how+does+mvc+save+view+state&cd=8&hl=en&ct=clnk&gl=us
There are a lot of articles about view states in MVC and since you're new to this perhaps some pre-reading would be handy and may even answer your upcoming questions!
Good luck!

Related

rails 3 - Add a Button that won't submit a form

I am trying to add a few "next" and "back" buttons to a form. The Idea is to divide the filling-out process into several steps and with these buttons, the div of the current step gets hidden and the next resp. previous step is displayed.
My Problem is that when I add buttons in the following way...
<button class="proceed_button" id="loan_information">Proceed</button>
<button class="cancel_button" id="loan_information">Cancel</button>
... they submit the form.
Is every button inside a form-tag considered to be a submit-button?
If so, how can I change this behavior?
If not, why are they doing it then?
Ok, the solution is that the button needs a type.
<button type="button" class="proceed_button" id="loan_information">Proceed</button>
<button type="button" class="cancel_button" id="loan_information">Cancel</button>
Like this, it won't submit the form anymore.
According to http://w3schools.com/html5/att_button_type.asp the default type is depending on the browser, so you should always specify the type.
I'm not sure that you want a button, maybe you want it to look like a button. Either way, refer to this post: rails 3: display link as button?
Once you have your button, you'll need to update your javascript to prevent anything from happening when it's clicked (assuming you have jquery). It's still nice to provide a real fallback for those dinosaurs without js, so assuming your proceed button submits for users without js, for those with js you'd do something like:
$('#proceed_button').click(function(e) { e.preventDefault(); // Show and hide your divs here });
Also note that in your posted code you should not have two buttons with the same id, your ids and classes look swapped.

How to create a "New xxx" popup?

I have a Grid object and added a [ (+) New Client ] button which I'd like to open a popup form to create the new client with a couple fields.
I've looked at the code examples in the website but haven't found how to do it (sorry if I've missed something).
This is the current page code:
function page_clients_listing($p){
$g = $p->add('Grid');
$g->addColumn('text','first_name');
$g->addColumn('text','last_name');
$g->addColumn('inline','telephone');
$g->addColumn('expander','comments');
$g->setSource('client');
$g->addButton('With Icon')->set('Add New Client')->setIcon('Plus');
}
Thanks in advance!
You can either create a popup or a dialog. Dialog is based on jQuery UI dialog implementation. Popups are likely to be blocked and are harder to control.
This is actually working for any object (you can apply to view, button, image, icon, etc), but I'll use button).
$b=$g->addButton('Add New Client')->setIcon('Plus');
$b->js('click')->univ()->frameURL($title,$url);
// OR
$b->js('click')->univ()->dialogURL($title,$url);
$url would most likely be returned by api->getDestinationURL(). The other page would be loaded and scripts on that page will be evaluated. Let's say you are on other page and now need to close the window.
$result = $this->addButton('Close')->js('click')->univ()->closeDialog();
closeDialog() returns a jQuery chain object pointing to a view which originally opened the frame. As a result if you do $result->hide(); then after dialog is closed, the original button ('add new client') will also be hidden.
Here is example to show some additional things you can do with frames, reloading and custom event handlers:
http://agiletoolkit.org/example/refresh1

Retaining Tab controls data in MVC 2

I have a multiple tabs and I am calling an ascx file for each tab, I have some control and submit button for each ascx file. Now I when ever I enter the data in first tab and switch to second tab and enter some data and click on submit then the first tab data is also getting empty.
I want to keep the data for the first tab or when I enter data on the firrst tab and click submit then what ever the data is there in other tabs must be there.
Can any one some source how to resolve this problem.
MVC2 has no "viewstate" like web.forms does. That means if it's not on the screen in some way, you must store it in session to persist it. I wouldn't recomend using session as you can tie youreself in knots in some situations, but can be appropriate for some things.
You basically have 2 options left if you don't want to use session:
1 - Show your tabs all on 1 page and then show/hide them using jquery to the client
For example:
Tab 1
Tab 2
Tab 3
<div id="tabcontents1" class="tabcont">
...
</div>
<div id="tabcontents2" class="tabcont">
...
</div>
<div id="tabcontents3" class="tabcont">
...
</div>
Then use JQuery or javascript to hide them on load and show them on click. For example:
$(document).ready(function () {
$(".tabcont").hide();
});
function showTab(tab) {
$(".tabcont").hide();
$("#"+tab).show();
}
OR
2 - Put any data you want to persist in hidden inputs on your form. If you only have a small model, this can be the easiest approach.
NOTE: This rule is a HTML rule, not an MVC rule. Webforms just "fakes" persistance with ViewState and MVC is more honest about how you deal with http.

Add Tooltips to helper html.dropdownlist in asp.net mvc

I have a dropdownlist that displays just products codes 101,102,103
If the user hovers over a product code I would like the title to appear as a tooltip.
So it's not your normal DataValue & DataText scenerio, because "101" would be both the display text & the value.
I believe I will need to use jQuery to achive this effect.
And I also believe I would need to set the Title attribute of each list item as the Product Title.
My question is, using helper html.dropdownlist how can I set the title attribute?
Thanks, this is my first day using MVC
We used tipsy once for something very similar. http://plugins.jquery.com/project/tipsy
We actually took it a step further and "created" an attribute like this
<span class="tipsyItem" tipsy="This is my tooltip text!">This is my regular text!</span>
The reason was that tipsy would still show the regular tooltip on occasion. We used some of the advanced settings like this.
$('.tipsyItem').tipsy({title: 'tipsy'});
Plus you can theme it. Also were even able to get it to support html embedded in the tooltips for links and such.
$('.tipsyItem').tipsy({html: true });
Twitter used to use tipsy. I'm not sure if they still do.

ExtJS: disable checkbox toggle on label click

I am designing a checkbox for a for and I absolutely cannot have the checkbox to toggle when the user clicks on its label, as this label contains a link to open a small infobox where the user gets to know what he or she is accepting by selecting the checkbox.
How can I disable checkbox toggle when clicking on its label?
The code looks simply like this (this element is inside a FormPanel items list:)
{
xtype:'checkbox',
id: 'privacyCheck',
fieldLabel: 'I have read, understood and accepted the privacy policy of ABCDE'
}
Instead of using the boxLabel property or field label on the checkbox, create a separate label object next to the checkbox. This should make it easier to manipulate your handler for the label. Otherwise, you will need to dig through the appropriate DOM element for the boxLabel (not pretty) to get at it.
I know, this topic is rather old, but I found it, searching for a solution to the exact same problem. So I'd like to share.
I needed to modify the browsers behaviour to mimick the behaviour of a legacy site, while making said site "accessible". (The for-attribute of the label tag is needed and a label without a for-attribute can not be used.)
I don't know about ExtJS, but since the legacy site uses jQuery in the frontend, I solved the problem this way:
//[...]
$(document).ready(function () {
$('.donotToogleCheckbox').click(function (event) {
event.preventDefault();
// do other stuff like displaying a dialog or something
})
});
//[...]
<label class='donotToogleCheckbox' for='myCheckbox'>DaLabel</label>
<input id='myCheckbox' name='myCheckbox' type="checkbox">
//[...]