How can I get just href part of Html.ActionLink result text - asp.net-mvc

As you know,
<%=Html.ActionLink("Back to List", "Index") %>
generates html like this : Back To List
But I need just href part.
I will use it in JS code and I do not want to write manually.
Can I gerenate what I need part ?

Try this
<%=Url.Action("Action","Controller")%>

Mathias's answer is what I use. ASP.NET MVC 2 gives you strongly types Url.Action too.
I find this most useful in javascript so:
<script type="text/javascript">
var urlToPostTo = '<%= Url.Action<HomeController>(h => h.ContactUs()) %>';
var someData = 'Some valuable data!';
$.post(urlToPostTo, someData, function()
{
alert('Successfully posted some data to some url');
});
</script>
This allows you to avoid putting hardcoded paths in your markup, leaving you with a slightly more maintainable solution.
That said, I'm still hoping that these will be compile time checked as normal when MVC 2 is finally released.

Related

footer HTML inside Javascript

Hello i'm using the following javascript code which will be in a separate file so that it can be referenced globally across the site.
It allows me to have a global reference for html elements such as footers and menus and include them by id:
<body>
<div id="copyright"></div>
<script>
var copyright = document.getElementById("copyright");
copyright.innerHTML = "<p>© Some Company 2013</p>";
</script>
</body>
This works ok as long as the javascript is placed at the bottom of the page. I was wondering what i might need to modify to allow me to move the script into the header?
Many thanks.
Run the code on DOM ready. Not fun without a library like jQuery, but I have to ask why you're doing this at all.
There are far, far better ways to reuse HTML: templating.
Okay, so you're using jQuery. Then you can use the usual, pervasive document ready handling:
$(document).ready(function ()
{
$('#copyright').html('<p>© Some Company 2013</p>');
});
// or the short-hand version
$(function ()
{
$('#copyright').html('<p>© Some Company 2013</p>');
});
That said, I still recommend templating instead. You can even do it client-side, with jQuery.

Rails, number_field_tag direct goto record

I'm looking for a way, how a user can type the ID of a record (i.e. /controller/23) , then press the goto button and it navigates directly to /controller/id
So the user is on the index page. There is something like a number_field_tag (input field), wrapped inside a form? You type the ID of the record and then click GOTO.
I don't want to explain to a user they can do this simply by changing the URL, so i prefer a solution that uses the page.
Preferable non javascript. Any ideas?
Thanks.
You'll either need to write another action to handle the translation of submission -> redirection to the URL cleanly or use a javascript solution in my opinion.
<script type="text/javascript">
$('form') // replace with a more detailed selector of your form (e.g. #formid)
.submit(function() {
window.location.href = 'http://foo.bar/controller/' + $('#field-id').val();
return false;
});
</script>
That bit of JS will accomplish what you need if you decided that a JS approach is satisfactory.
I think this might do
<%= link_to "GOTO" XXX_path(params[:id_of_field]), :class=>"STYLE_AS_BUTTON" %>

ASP.NET MVC Scripting in js files

I still find myself having to declare global variables in my .aspx files (the project was started before razor). For example I have to declare global javascript variables like this in my .aspx files:
var getDistributionListUrl = '<%= Url.Action("GetDistributionList", "PublicDocument") %>';
I can then reference this variable in my .js files.
Is there a better way?
Is there a better way?
There is nothing wrong with this.
Personally I use HTML5 data-* attributes on some DOM elements that I am manipulating later in my scripts. For example:
<div id="foo" data-url="<%= Url.Action("GetDistributionList", "PublicDocument") %>">
Hello
</div>
and then in my js:
$('#foo').click(function() {
var url = $(this).data('url');
...
});
But in 99.99% of the cases those urls are associated with either <form> elements or link hrefs so in my javascript I simply retrieve and use this value when I need to do some progressive enhancement on the given DOM element (like unobtrusively AJAXifying forms or anchors).

Serving static content to my action using MVC's convention approach

I'm looking at outsourcing some in-page help on a large web application I am creating and would like to make it really easy for this content to added to our pages when we're ready for it.
So I was thinking I could create a system where I can add text files to the same folder where an action expects it's view to be and read out the content of that file the content in that file can be passed back to the view to display. Either that or create a helper that would do the same.
Example
Controllers
HomeController.cs
Views
Home
Index.aspx
Index.txt
Index.aspx would then have access to the content in Index.txt.
How would I start going about creating this system. Are there any built in classes in .NET MVC that I could take advantage of?
A similar question was asked yesterday: Including static html file from ~/Content into ASP.NET MVC view.
Basically you can read the text from the file and include it inside your view by using File.ReadAllText so you would have something like this inside your index.aspx file
<%= File.ReadAllText(Server.MapPath("~/Views/Home/index.txt")) %>
I'd create a parallel hierarchy in the Content folder and put the files there, probably as HTML. Then you can simply load them via AJAX in the view using the parallel hierarchy convention.
Content
Help
Home
index-help.html
about-help.html
Foo
index-help.html
bar-help.html
Then in your views
<div class="help">
<noscript>
<a href='#Url.Content( "~/content/help/home/index-help.html" )'>Click for Help</a>
</noscript>
</div>
<script type="text/javascript">
$(function() {
$('.help').load( '#Url.Content( "~/content/help/home/index-help.html" )' );
});
</script>
You may also be able to extract the controller/action from RouteData in the view if your routes are consistent and move this to your _Layout.cshtml file with the path being provided by route data.
#{
var controller = ViewContext.RouteData["controller"] as string;
var action = ViewContext.RouteData["action"] as string;
var url = Url.Content( string.Format( "~/content/help/{0}/{1}-help.html", controller, action ) );
<div class="help">
<noscript>
<a href="#url>Click for Help</a>
</noscript>
</div>
<script type="text/javascript">
$(function() {
$('.help').load( "#url" );
});
</script>
}
One possible solution would be to store them as xml file instead, that are serialized from the model the view is expecting. You could then create an Action Filter populate the model being returned with the data from the XML file. I hope that helps.

asp.net mvc: simulating autopostback for simple checkbox

I have a simple checkbox, generated with:
<%= Html.CheckBox("myCB" )%>
How can I add an onChange handler to this that does a submit?
Add an onClick handler to the CheckBox that submits the form the CheckBox belongs to...quick, clickHandler codeless example:
<%= Html.CheckBox("myCB",
new { onClick = "$(this).parent('form:first').submit();" });
(example definitely not checked for accuracy)
If you have only one form, and are not using JQuery (you should be, by the way) try this:
<%= Html.CheckBox("myCB",
new { onClick = "document.form.submit();" });
I would highly recommend using jQuery to support this because it makes it easier to add the behavior to a checkbox throughout your site by having the selector either be ID or class-based. Then you could put the script anywhere on the page or in an external .js file.
<script language="javascript" type="text/javascript">
$('#myCB').click(function() { $(this).parent('form:first').submit(); });
</script>
Alternatively, the selector could be class-based (or any attribute for that matter). More info here: http://docs.jquery.com/Selectors

Resources