Linking to a MVC controller from ASP page - asp.net

I have inherited a asp project with all new projects being done in MVC. I have a link on an asp page which I would like to route to a MVC action but can't seem to manage this. This is what I have so far which doesn't work.
ASP page:
<div id="mainMenuAnalystics" class="panel pageLink" runat="server">
<div class="header">
<asp:HyperLink NavigateUrl="IDoNotExist.aspx"
Text="Analytics"
runat="server"/><br class="floatClear" />
</div>
</div>
In the routeConfig.cs file I added this:
routes.MapRoute(
name: "Analytics",
url: "DestinyWireless.src/aspx/IDoNotExist.aspx",
defaults:
new
{
controller = "Areas/MVC/Controllers/ReportingController",
action = "Index"
});
So instead of going to IdoNotexist.aspx, I want to redirect to the Index method in Reporting controller which is located at Areas/MVC/Controllers/ReportingController. Anyone know how to achieve this?
Of course it would be great if I could just add a #Url.action on the asp page itself but it seems asp does not support razor.
Folder structure:

Related

How to post to a Razor PageModel from partial view?

I'm building a website which has modal login and registrations forms.
The issue is when I'm trying to submit the form filled by the user, the OnPost of the corresponding razor models are not being called. I also tried using the Identity models as the models for the views and those are not being called as well.
Is there a way to do this correctly?
In your Partial view you need to point the form to the page which has the OnPost handle.
<form asp-page="/YourPage" method="post">
...inputs...
<button type="submit">Submit</button>
</form>
Make sure your OnPost accepts a a parameter of the view model like so:
OnPost(MyViewModel vm)
Realised you came after this question
Assuming you have the LoginViewModel as a property inside your razor page.
Instead of using
<partial name="_LoginPartial"
model='new LoginViewModel { InputModel = new InputModel() }' />
you want to use
<partial name="_LoginPartial" for='LoginViewModel' />

ASP.Net Web Api 2.2 generated help files not displaying api documentation

I have followed the steps to generate api help here http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/creating-api-help-pages and this has been working fine.
Today, I've noticed that the Help page is not displaying anything in the api groups section.
This line;
ILookup<HttpControllerDescriptor, ApiDescription> apiGroups = Model.ToLookup(api => api.ActionDescriptor.ControllerDescriptor);
is not finding any groups to iterate through and display the api help for.
#using System.Web.Http
#using System.Web.Http.Controllers
#using System.Web.Http.Description
#using System.Collections.ObjectModel
#using MyCompany.WebApi.Areas.HelpPage.Models
#model Collection<ApiDescription>
#{
ViewBag.Title = "Web API Help Page";
// Group APIs by controller
ILookup<HttpControllerDescriptor, ApiDescription> apiGroups = Model.ToLookup(api => api.ActionDescriptor.ControllerDescriptor);
}
<link type="text/css" href="~/Areas/HelpPage/HelpPage.css" rel="stylesheet" />
<header class="help-page">
<div class="content-wrapper">
<div class="float-left">
<h1>#ViewBag.Title</h1>
</div>
</div>
</header>
<div id="body" class="help-page">
<section class="featured">
<div class="content-wrapper">
<h2>Introduction</h2>
<p>
Provide a general description of your APIs here.
</p>
</div>
</section>
<section class="content-wrapper main-content clear-fix">
#foreach (var group in apiGroups)
{
#Html.DisplayFor(m => group, "ApiGroup")
}
</section>
</div>
I'm using Microsoft.AspNet.WebApi.HelpPage version 5.2.3, and same issue with 5.2.2. Visual Studio 2015, MVC 5.
Any ideas?
Ok it was a conflict between some code in StartUp.cs and Global.asax.cs
Normally in Global.asax you have this line;
GlobalConfiguration.Configure(WebApiConfig.Register);
but I had commented this out because of a bunch of changes I had done to use OWIN and get CORS configured. So my StartUp.cs looked like this;
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
//Load any config settings from web.config
MyConfiguration.Load();
//Create AutoMapper maps.
AutoMapperConfigurator.Configure();
//write web api into Owin server pipeline.
HttpConfiguration config = new HttpConfiguration();
WebApiConfig.Register(config);
//Get CORS working.
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
}
and I thought as I'm calling WebApiConfig.Register in here I didn't need to call it in Global.asax.
Clearly, to get the api help working and generated I do still need to call GlobalConfiguration.Configure(WebApiConfig.Register) in the Application_Start method.
Hope that helps.

Submitting form in angularjs in asp.net web page

I am hating asp.net...
I have a really simple web site. In my master page I have a form runat="server".. I need it because in some page I have server side controls (login control for example, or my custom control)..
Now I have a simple page, with a really simple form. And in this page, I would like to use angularjs..
What I want is that (simply) when I submit this angularjs form, I arrive on the server just when the form is valid.
In this page I have tried different solution but no solution works!
The problem I think is that I have a (angularjs) form (in the page, this is not runat="server") that is nested in asp.netform (in the master page, runat="server")..
Here what I tried:
1) remove the asp.net form from the master page and add different asp.net form just where i need, but in some page i had more than one form with `runat="server" and this is not possible:
two form runat=server
2)remove the angularjs form, don't do a submit, but manage a simple ng-click event, but in this way I go on server always, even if form is not valid...
3)leave the angularjs form a do a submit, but in this way I obtain an "invalid postback error"... I have tired this solution but it does not work:
Invalid postback error
What I simply want is to use angularjs and go on server just when form is valid... How can I do? Thank you..
UPDATE:
My MasterPage is:
<body>
<form runat="server">
...
<asp:ContentPlaceHolder ID="content" runat="server"></asp:ContentPlaceHolder>
...
</form>
</body>
My Page is:
<asp:Content ID="Content3" ContentPlaceHolderID="content" runat="server">
<script type="text/javascript" src="/Scripts/angular.min.js"></script>
<script type="text/javascript" src="/Scripts/angular-resource.min.js"></script>
<script type="text/javascript">
(function (angular) {
var public_area = angular.module("public-area", ['ngResource']);
public_area.factory("PresentaUnAmicoService", function ($resource) {
return {
piani_tariffari: $resource("/api/PresentaUnAmico/")
};
});
public_area.controller("PresentaUnAmicoController", function ($scope, PresentaUnAmicoService) {
$scope.sendInvitation = function ()
{
var invitation =
new PresentaUnAmicoService
.piani_tariffari($scope)
.$save();
}
});
})(angular);
</script>
<div data-ng-app="public-area" data-ng-controller="PresentaUnAmicoController">
<form name="presenta_amico-form" novalidate>
<table class="three_column" data-ng-class="{ 'ng-submitted' : submitted }">
... //Here there is my form
<button type="button" class="button link" id="btnSendInvitation" runat="server" data-ng-click="submitted=true" ng-submit="sendInvitation()">
<span><b>INVIA EMAIL</b></span> <img class="middle" src="/Styles/img/continue_24x24.png" />
</button>
...
</table>
</form>
</div>
</asp:Content>
I Know, It's not correct have a form inside another form.. but I am looking for a solution...
What you need in order to nest form is to use
eg:
<form runat="server">
<ng-form name="myFormName" ...>
//Your Code
</ng-form>
</form>
have a look here

MVC Html.BeginForm CSS

My form is not rendering css correctly in Visual Studio 2013. I am using the following code to declare the form:
#using (Html.BeginForm())
I have tried using this instead but still no joy:
#using (Html.BeginForm(new {#id="contact-form"}))
My previous HTML5 used to look like this:
<form id="contact-form" action" name="contact-form" method="POST" accept-charset="utf-8">
But now, the MVC generated code is:
<form action="/Home/Contact/contact-form" method="post"><div class="validation-summary-valid" data-valmsg-summary="true"><ul><li style="display:none"></li></ul></div>
There is no sign of the id tag and no CSS applied to the form, also no clue where the extra code div class="validation-summary-valid" data-valmsg-summary="true"> came from.
Any help would be much appreciated :-)
Solved, the following code passed the CSS ID selector correctly:
#using (Html.BeginForm("Contact", "Home", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { #id = "contact-form", role = "form" }))

asp.net mvc javascript postback

I'm mixing asp.net webforms and asp.net-mvc. To use webforms I've included
routes.IgnoreRoute("Reports/{*pathInfo}");
in the public static void RegisterRoutes(RouteCollection routes) method.
It seems to work just fine. But javascript postbacks on the asp.net webform pages don't work. Specifically
<script type="text/javascript">
function callMethod(methodName, methodArgument)
{
alert('test1');
document.getElementById("methodname").value=methodName;
document.getElementById("methodargument").value=methodArgument;
alert('test2');
document.forms[0].submit();
}
</script>
doesn't work. Everything is fine until the document.forms[0].submit(); call which appears to do nothing. If I completely disable the asp.net MVC route mapping then the above Javascript works just fine.
I just completed a brand new sample project and was able to get this to work... I created a folder in the root of my project called Reports and added the Billing.aspx page there. I then added the code below to the default Index view within the Home folder as shown below.
Global.asax
routes.IgnoreRoute("Reports/{*pathInfo}");
MVC Page Views\Home\Index.aspx
<form method="post" action="../../Reports/Billing.aspx?tenantId=0003-0140&rentNum=0" id="myForm">
<input type="text" id="sample" /><br />
<input type="button" onclick="callMethod();" value="send" />
</form>
<script type="text/javascript">
function callMethod()
{
alert('test1');
alert(document.getElementById("sample").value);
alert('test2');
document.forms[0].submit();
}
</script>
My guess is that even though your form's action is set to Billing.aspx, it is not looking for it in the correct folder. Try adding "../../Reports/" in front of Billing.aspx to your form's action. Since the Billing page is not in the same root as the MVC page, that is likely to not go anywhere on a post action...

Resources