Is passing parameters for controller overload from view good practice in .Net MVC - asp.net-mvc-2

So right now I have a partial view that just has a list of possible actions, here is the main portion:
<%= Html.ActionLink("Show", "Show", new { id = dbId, css="/Content/Site.css" }) %> |
<%= Html.ActionLink("Edit", "Edit", new { id = dbId }) %> |
<%= Html.ActionLink("Delete", "Delete", new { id = dbId }, new { #class = "deleteLink" })%>|
<%= Html.ActionLink("Print", "Show", new { id = dbId, css="/Content/Other.css"}) %>
Essentially, there is a Show and and Print option, I decided it would be more convenient to not create a new view for print, and just overload the Show Action by passing an extra parameter that would tell which CSS to display, and then use ViewData to set the CSS for the view.
Is this good practice?
Should I create a new view? They are completely the same except for the CSS.

If the css is the only thing that is different, you could just include the css and set the media type to be picked up when the user prints from the browser, e.g.:
<style media="print" ...

Related

Rails: Replace Submit Button With Pure HTML

I have a simple form with several hidden fields:
= form_for item.votes.new, remote: true do |f|
= f.hidden_field :value, value; 1
= f.hidden_field :voteable_id, value: item.id
= f.hidden_field :voteable_type, value: item.class.name
= f.submit
Instead of a submit button, however, I want to use a div:
div.arrow.arrow--up
I know this is possible using javascript, but can I somehow manage this purely with Rails? For instance, something like:
= f.submit "", type: :html, partial: 'up_arrow'
As far as I know, you have to use the submit tag or javascript. You can, however, use CSS to format the submit button like any other html element.
http://eisabainyo.net/weblog/2009/04/24/how-to-style-a-submit-button-in-css/

Liferay AUI Autocomplete limit selection

During a form compilation i need to be able to insert a value, presenting a list of already used values (usual listbox behavior).
But i also need to be able to enter a new value, so i choosed aui:autocomplete (code below).
By default it allows selection of multiple items separated by the "delimChar" separator.
Is there any way to limit to 1 single value selection (or new value)?
var catArray = new Array();
<%for (String s : categoryList) {%>
catArray.push('<%=s%>');
<%} %>
var autoCompleteCategory = new A.AutoComplete(
{
contentBox: '#<portlet:namespace />contactOptions',
input:'#<portlet:namespace/>category',
dataSource: catArray,
delimChar: ',',
typeAhead: true,
}).render();
Making comment as the answer for better visibility:
You can remove the attribute delimchar: ',' from the following code:
var catArray = new Array();
<%for (String s : categoryList) {%>
catArray.push('<%=s%>');
<%} %>
var autoCompleteCategory = new A.AutoComplete(
{
contentBox: '#<portlet:namespace />contactOptions',
input:'#<portlet:namespace/>category',
dataSource: catArray,
delimChar: ',', // **remove this attribute**
typeAhead: true,
}).render();
I think this should work.
Also clear your browser cache before trying it :)

Insert images into the menu list

I'm trying to populate my menu items with an image/logo on each of the third menu list, please see example below.
I've been using Silverstripe to populate the menu items, below is my code so far without the images. Can someone please point me to the right direction on how I go about inserting the images/logos on the third level menu?
// initialise plugins
jQuery(function(){
jQuery('ul.sf-menu').superfish();
});
</script>
<ul class="sf-menu">
<% control Menu(1) %>
<li>
$MenuTitle
<% if Children %>
<ul><% control Children %>
<li>
$MenuTitle
<% if Children %>
<ul><% control Children %>
<li>
$MenuTitle
</li>
<% end_control %>
</ul><% end_if %>
</li>
<% end_control %>
</ul><% end_if %>
<!--<li>
menu item
</li>-->
</li> <!--current-->
<% end_control %><!-- <li>
menu item
</li> -->
</ul> <!--sf-menu-->
Thanks heaps.
S:)
UPDATE below is my Page.php, and I have insert $Image.SetSize(20,20) $MenuTitle to my third level menu. However everytime i tried to insert an image through CMS, there is error coming up in CMS. Sorry i'm new to this, any help would be appreciated.
<?php
class Page extends SiteTree {
public static $db = array(
);
public static $has_one = array(
'MenuImage' => 'Image'
);
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.Content.Images", new ImageField('MenuImage','Menu image'));
return $fields;
}
}
class Page_Controller extends ContentController {
public static $allowed_actions = array (
);
public function init() {
parent::init();
// Note: you should use SS template require tags inside your templates
// instead of putting Requirements calls here. However these are
// included so that our older themes still work
Requirements::themedCSS('layout');
Requirements::themedCSS('typography');
Requirements::themedCSS('form');
}
}
here is the error info.
[User Error] Couldn't run query: SELECT * FROM "Page" WHERE "ID" = 15 Table 'ss_show.page' doesn't exist
POST /Show/admin/EditForm/field/MenuImage/EditFileForm
Line 525 in C:\wamp\www\Show\sapphire\core\model\MySQLDatabase.php
Assuming you have an image on your page object, you can render it in your template by doing something like this:
$MyImage.SetWidth(50) $MenuTitle
"$MyImage.SetWidth(50)" will output an image tag with an image resized to 50px wide. See the SilverStripe image reference for more info.
In case you don't have the menu image yet, follow the official documentation to do so: http://doc.silverstripe.org/sapphire/en/tutorials/2-extending-a-basic-site
I figured it out with soneone's help. Just in case others are in the same situation, it is because I didn't rebuild my database. Visit mysite.com/dev/build to rebuild the database. The code above in the update works after that. Thanks everyone! :)

Restrict selection to single select of a listboxFor- MVC 2

I am using following code to generate a list box..
<%: Html.ListBoxFor(m => m.Subscribers, new List<SelectListItem>(), new { #class = "list_style_Wizard" })%>
But we can select more than one items from the listbox.. How can i restric it to single select ???
The HTML helpers DropDownListFor and ListBoxFor seem to add the multiple attribute when rendering as a listbox. I use a combination of the DropDownListFor/ListBoxFor and a jQuery livequery selector to remove the multiple attribute. In Razor use:
#Html.DropDownListFor(m => m.SelectedId, Model.SelectList,
new { size = 10, #class = "selectOneListBox" })
and in JavaScript:
$(".selectOneListBox").livequery(function () {
$(this).removeAttr('multiple');
});
I'm sure you could also write your own version of the HTML helper routine that doesn't spit out the multiple attribute.
Html.ListBoxFor is used to render a multiple choice list box. For single choice use
Html.DropDownListFor
<%: Html.DropDownListFor(m => m.Subscribers, listOfsubscribers, new { #class = "list_style_Wizard" }) %>

Html.ActionLink with id value from a dropdownlist

I've got a dropdownlist:
<%= Html.DropDownList("ddlNames", new SelectList(Model.NameList, "ID", "Name"))%>
I've got an ActionLink:
<%: Html.ActionLink("edit", "Edit", "Members", new { area = "MembersArea", id = XXX }, null)%>
I want the value of the dropdownlist in the XXX.
So I want to use values from controls on a view in the ActionLink.
Is that possible in a simple manner?
thanks,
Filip
You can't do this because the html helpers execute at the server side while the dropdown value can change at the client side. The only way to achieve it is to use javascript. You could register for the onchange event of the dropdown and modify the value of the href of the anchor:
$(function() {
$('#ddlNames').change(function() {
var value = this.value; // get the selected value
// TODO: modify the value of the anchor
});
});
This is probably not the best solution because the routes are configured on the server side and in order to modify the value of the link you need to do some string manipulation on the client side.
As an alternative you could use a form and a submit button instead of an anchor. This way the selected value of the dropdown will be automatically sent to the server and you don't need any javascript:
<% using (Html.BeginForm("Edit", "Members", new { area = "MembersArea" })) { %>
<%= Html.DropDownListFor(x => x.SelectedName,
new SelectList(Model.NameList, "ID", "Name"))%>
<input type="submit" value="Edit" />
<% } %>
Instead of modifying the value of the anchor every time a relevant dropdown is changed, just modify it once, on click.
Example using Razor:
#Html.DropDownList("DropDownFirstNames", new SelectList(Model.FirstNames, "ID", "Name"))
#Html.DropDownList("DropDownLastNames", new SelectList(Model.LastNames, "ID", "Name"))
#Html.ActionLink("Submit name", "ActionName", "ControllerName", null, new { #id = "SubmitName" })
<script type="text/javascript">
$('#SubmitName').click(function () {
var first = $('#DropDownFirstNames').val();
var last = $('#DropDownLastNames').val();
var path = '#Url.Content("~/ControllerName/ActionName")' + "?firstId=" + first + "+&lastId=" + last
$(this).attr("href", path);
});
</script>