How to use ScriptManager with Razor in MVC 4? - asp.net-mvc

I used ScriptManager in .aspx page to Maintain URL history in .Net i.e.
on aspx page i put this script after form tag
> <asp:ScriptManager runat="server" ID="ScriptManager1" EnablePartialRendering="true"
> EnableHistory="true">
> </asp:ScriptManager>
and in my .js file i put this code
Sys.Application.add_navigate(function (sender, e) {
navigate(sender, e);
});
Sys.Application.addHistoryPoint(objOut, null);
Now same thing i want to use in MVC 4 with Razor
I used MicrosoftAjax.js for ScriptManager and .js code is same.
but i am getting issue on callback.
let me explain you with example:
suppose Actual URL is
www.websitename.com/cat/30/
anchor tag link on page is
www.websitename.com/cat/30/?q=10
once callback perform anchor tag link becomes
www.websitename.com/?q=10
I mean after callback "cat/30/" is missing from link.
can anyone tell me what I am doing wrong.
Thanks.
Ashu

ScriptManager is part of ASP.NET Webforms. It is not available in ASP.NET MVC. They are two completely different frameworks.
There is a port of ScriptManager to ASP.NET MVC which is available here. It might provide what you are looking for.

Related

How to include shieldui in asp.net mvc?

I'm new to shieldui js and asp.net mvc and I'm doing some examples for a train. I have no problem including the js in a html file but I'm struggling to include it in asp.net mvc. Lets say i have jquery and shield ui in Scripts folder. I did reference Shield.Mvc.UI but I still cant access them via #(Html.XXXX).
Lets say i want to create ShieldCalendar using View.cshtml. The example is:
<div class="container">
#(Html.ShieldCalendar()
.Name("calendar")
.HtmlAttribute("class", "calendar")
.Min(new DateTime(2009, 2, 23))
.Max(new DateTime(2039, 3, 1))
.Value(DateTime.Now)
.Footer(fb => fb.Enabled(true).FooterTemlpate("{0:dd.MM.yy}")))
I'm a beginner and I may miss something fundamental for asp.net MVC.
Later findings: it looks like some time ago there weren't html helper extension methods for that. Are you sure you have something like that?
Question reference: shield ui chart: generate series dynamically? (where op is saying that he wrote a custom html helper for .net mvc)
Html helpers reference: http://www.tutorialsteacher.com/mvc/html-helpers
Are you sure you have html helpers in Shield.Web.UI namespace? (see this question too: Referencing the Shield UI ASP.NET MVC javascript modules)
1st approach
You can create a bundle with those two javascript files for now.
Open the App_Start\BundleConfig.cs, there you will have a RegisterBundles method where you can create a bundle with those two files.
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jqueryand")
.Include("~/Scripts/jquery-3.1.1.js"))
.Include("~/Scripts/shieldui.js");
// i don't know exactly the name of the library
}
And in your Global.asax.cs, you need to register this bundle:
In Application_Start method, add the following line:
BundleConfig.RegisterBundles(BundleTable.Bundles);
Then in your _Layout or your more specific view, you'll have to add #Scripts.Render("~/bundles/jquery")
More about this approach here: https://www.asp.net/mvc/overview/performance/bundling-and-minification
And here:
How do I add BundleConfig.cs to my project? (probably this question is very useful)
2nd approach
Use something like RequireJs for .NET, which will bring some features to your application, but will also add some complexity.
See my answer for the question below:
RequireJS - ASP.NET MVC Bundle Script
About this part of your problem:
but I still cant access them via #(Html.XXXX).
Maybe your intellisense is broken, you can give it a try after you add the script to the page/layout and after you make sure that you have
#using Shield.Web.UI
for your page/layout.

Helper for PagedList supporting unobtrusive Ajax in ASP.NET MVC

I'm currently using PagedList (https://github.com/TroyGoode/PagedList/) to manage my paging in an ASP.NET MVC application.
As of today I have started converting some parts of the application to use AJAX, which ASP.NET MVC makes quite easy.
The first problem I have run into however is that the PagedList.MVC helper #Html.PagedListPager is not in any way compatable with unobtrusive AJAX.
All I really need to do is add some attributes to the paging links (see below) and the rest would be taken care of automatically. PagedListPager does not provide any way to do this however.
data-ajax="true" data-ajax-mode="replace" data-ajax-update="#SearchResults"
Has anyone run into this and found an elegant solution?
I have added support for unobtrusive AJAX:
https://github.com/TroyGoode/PagedList/issues/26#issuecomment-6471793
I believe this may be the most elegant solution.
#Html.PagedListPager((IPagedList)Model.Articles, page => Url.Action("Index", new { s = Model.SearchString, page = page }))
<script>
var pages = $('#pages a[href^="/"]');
pages.attr('data-ajax', 'true')
.attr('data-ajax-mode', 'replace')
.attr('data-ajax-update', '#SearchResults')
.attr('data-ajax-method', 'post');
</script>
Quick jQuery hack to add the necessary attributes to all links in order for them to be picked up by the unobtrusive ajax module.
The [href^="/"] part ensures that only the clickble links will be modified. If you don't use this, the greyed out Previous link will be clickable.

html.beginform v old skool form tag

What is the benefit to using Html.BeginForm?
We're having some issues related to html helpers and razor generator view testing and I'm struggling to see the benefit which would stop us going back to old skool form tags.
Has anyone got an argument for or against either?
by old skool i mean:
<form action="#Url.Action('Blah')">
The Html.BeginForm is useful because it generates the url using the routes defined in the Global.asax. (or you can extend it with your own code)
Using the old tag is neither worst or best in my opinion. You simply have to generate your url manually or using the Url helper. In the end the html in the page will be the same
<form ....>
html
</form>
Html.BeginForm also implements IDisposable, meaning the form must be closed properly. It's a minor thing, perhaps, but not closing Html.BeginForm produces a run-time error, where an unclosed <form> tag does not.
no there is no difference , the form tag just use the routing to generate the url , so if you use #Url.Action you are good to go
there is even books use that way a plain old tag and a url helper to generate the route
ASP.NET MVC Website Programming is an example
Edit
**
starting from Mvc 4 there is no difference , prior to Mvc 4 , Mvc 3 for example require the Html.BeginForm to make the javascript unobtrusive validation to work

How to include contents of plain HTML or classic ASP file in an ASP.net MVC 3 view?

I'm working on an ASP.net MVC 3 application. I have an external classic ASP file that renders a fragment of a page.
I understand that in ASP.net MVC 3, I can define a partial view to contain a fragment of HTML that I want to reuse on multiple pages.
The basic idea that I have is that I would like to use an external classic ASP file to render a fragment of HTML in my ASP.net MVC 3 application.
I would like to use it with something like Html.Partial, but that won't work because a classic ASP file is obviously not an MVC partial view.
Another way of doing this would be to add the content of the file to the page with AJAX, but I don't want to add the overhead of another AJAX call to my page. What might be the solution I'm looking for?
Plain HTML: Just read it from disk and output it with #Html.Raw().
Asp or other dynamically created content: You may use HttpWebRequest to get the html markup, and then insert into you own view. You may want to cache the response.
For convenience, you may create extension methods for both methods.
Create a Controller Action that makes the external call and returns the content:
Html.RenderAction("GetContent","ExternalASP"); //GetContent- Action, ExternalASP- Controller

ASP.NET MVC editor template javascript location

We have an editor template that contains approx 40 lines of jquery. I tried dropping this script into a <asp:Content> block to keep all of the javascript in one location within the page. However, I get the following error message content controls have to be top-level controls in a content page.
Is there any way to get this working so we don't have script dotted around our final output pages or could someone recommend the best practice for storing javascript used within ASP.NET MVC templates? At the moment I'm thinking of pulling the code into a separate file and referencing it within the master page but this means it gets pulled into every page which isn't really ideal.
Thanks in advance.
It would be easier for later maintenance, if you keep the javascript into a separate file and reference it where ever it is needed. Also, if you feel that placing all script into a single file will increase unnecessary loading of scripts, where not needed, then break the scripts into separate files based on functionality/modules in which it is useful etc. strategy and then reference them instead.
Also, it was said that always, keep/reference the scripts at the bottom of the page so that page loading will be faster.
as siva says, bottom of the page is the 'ideal'. however, as for seperate files. this is only going to be practical as long as you don't reference asp.net elements from the page - ie:
<asp:Content ContentPlaceHolderID="jsCode" ID="jsCode1" runat="server">
<script type="text/javascript">
$(document).ready(function() {
getPoundsData();
});
function getPoundsData() {
var id = $("#ID").val();
var URL = '<%=Url.Action("GetPounds", "FundShareholder")%>';
var params = { id: id };
if (id != null)
SendAjaxCache("#" + $("#ShareholderID option:selected").text() + " RSP#", URL, params, null, ListDataShareholderPounds);
}
function ListDataShareholderPounds(data) {
if (data.length != 0) {
$('#shareholderPounds').html("");
$('#shareholderPounds').show();
$('#shareholderPounds').html(data);
}
};
</script>
</asp:Content>
notice the:
var URL = '<%=Url.Action("GetPounds", "FundShareholder")%>';
part in the js. what 'we' do is to add a content section to the master page at the very bottom to hold our js stuff. however, this only works inside the ViewPage (aspx) object. the ascx pages are 'ignorant' of any master page content sections.
We are currently working on systemizing the process whereby we save 'partial' js files with asp.net references inside them and then inject them into the page-flow via a filterattribute. we're at an early stage with this but the nice thing about this approach is that the partial js is treated as a file and is therefore cached for future visits to that page.
anyway, that's our current approach, would be interested to discover if peeps are using any similar mechanisms to inject js that contains asp.net object references.
cheers...
[edit] - here's a heads up on the approach i'm talking about (this wasn't our original inspiration, but is quite similar, tho is webforms, rather than mvc) - http://www.west-wind.com/WebLog/posts/252178.aspx or this one which IS mvc: http://poundingcode.blogspot.com/2009/12/injecting-javasript-into-aspnetmvc-with.html.
Finally found the article that inspired our 'search' in this: ASP.NET MVC routing and paths is js files plus http://codepaste.net/p2s3po

Resources