in my asp.net mvc project I want an Action Redirect to a url but I want the page to open in new window.
public ActionResult Index(int id)
{
...
return Redirect(page.Url);
}
To open up a new window on client side you will have to do something either in View HTML or use some javascript. This can not be done in controller.
In view, you can set the target property of your action link from where this action is being called.
You should specify that the link will open in a new window using the target attribute on <a> element in the view i.e.
<a href="#Url.Action("Index", "Controller", new{ area = "", id = 1 })"
target= "_blank">Link text</a>
In the server side you can retrun json (after an ajax call usually)
public ActionResult PreviewReport(Report rep)
{
return Json(new { type = "info", url = "..." }, JsonRequestBehavior.AllowGet);
}
In the client side you execute javascript (the result is from an ajax call to above action)
function (result) {
if (result.type === 'info') {
window.open(result.url, '_blank');
}
}
Related
Somehow I find this hard to describe, but here I go:
I have a div in my SelectClasses Razor view page with an id="id152".
In order for me to show that div on the page at reload, I have to add the suffix #id152 to my page url.
<div id="id152">blabla</div>
...
..
Section 7
Now my question: Is there a way to add/pass this suffix to a 'RedirectToAction()'?
public ActionResult Index()
{
//All we want to do is redirect to the class selection page and add a suffix
return RedirectToAction("SelectClasses", "Registration", new { id = 99 })); //add suffix here somewhere
}
So when my SelectClasses view is shown, the url looks something like this:
'[url]/SelectClasses/99#id152'
The RedirectToActionResult (among the rest of RedirectTo* results) is meant to be used for generation of URLs based on registered routing data.
In your case, you wish to concatenate a hash parameter value (#id152) that is not being sent to the server and only used by the browser. That's why said methods don't bother dealing with it.
I suggest you do this instead:
var redirUrl = Url.Action("SelectClasses", "Registration", new { id = 99 });
redirUrl = String.Concat(redirUrl, "#id152");
return Redirect(redirUrl);
I've got a MVC5 page with 2 links / buttons and 1 Grid. Due to some dynamic display problems I cannot have more than 1 grid, so I'm changing the binding of the grid to show either Company Info or Company Income.
That works great.
Now I've got an export control that will call function ExportData in my Controller, but for the function to know which data set to export, I need to tell which data is currently bound. Because the jquery is client-side and razor is server-side, I'm having some difficulty in telling razor the current value.
So I've added a parameter called 'typeOfContent', but how can I populate the value of this? In other words, in the code below, I want to add something where the ???? is (either CompanyInfo or CompanyIncome).
I don't think Razor will be able to read the value of the hidden HTML control?
Not sure how else I can tell ExportData which type of data is currently visible
#model int
Show Info
Show Income
....
#Html.Hidden("TypeOfContent", "Temp");
#using (Html.BeginForm("ExportData", "MyController",
new { CompanyID = Model, typeOfContent = ???? },
FormMethod.Post))
{
<div id="MyGrid">
#{Html.RenderAction("MyPartial", "MyController", new { CompanyID = Model});}
</div>
}
....
<script>
$(function () {
bindCompanyInfo();
$('#btn-link1').on('click', function (e) {
bindCompanyInfo();
$('#TypeOfContent').val("CompanyInfo");
})
$('#btn-link2').on('click', function (e) {
bindCompanyIncome();
$('#TypeOfContent').val("CompanyIncome");
})
function bindCompanyInfo() {
...
};
}
function bindCompanyIncome() {
...
};
}
I think this can allow you to set and reset the value with javascript and post ok.
#model int
Show Info
Show Income
....
#using (Html.BeginForm("ExportData", "MyController", FormMethod.Post))
{
#Html.Hidden("TypeOfContent");
#Html.Hidden("CompanyID", Model);
<div id="MyGrid">
#{Html.RenderAction("MyPartial", "MyController", new { CompanyID = Model });}
</div>
}
I have some code, where when the user clicks on the "x" icon then call the CancelPendingQuote action method passing along the requestId in the requestUrl. The action method is hitting but the value is not included in the requestIdEncrypted parameter, thus the action method parameter has a null value.
Pending List
#using (#Html.BeginForm("CancelPendingQuote", "Quote", new { requestIdEncrypted = request.RequestIdEncrypted }, FormMethod.Get, new { enctype = "multipart/form-data", #id = "removeRequest" }))
{
<span data-bind="click: function(data, event){ userWarning_Dialog('#removeRequest_dialog', event); }">
<img src="~/Areas/Waybill/Content/Images/Normal_Size/posta_delete_20px.png" />
<img src="~/Areas/Waybill/Content/Images/Normal_Size/posta_delete_mouseover_20px.png" style="display:none" />
</span>
}
Knockout userWarning function that submits the form. This is called when image "x" is clicked.
removeRequest: function (model, event)
{
var form = $("#removeRequest").closest("form");
$(form).submit().error(function (messageObj) {
// if fail return error message
$(".information-bar").trigger("ErrorText", messageObj.message);
});
$("#removeRequest_dialog").dialog("close");
},
Action method
[Authorize]
public ActionResult CancelPendingQuote(string requestIdEncrypted)
{
int requestId = Convert.ToInt16(Decryption.Decrypt(requestIdEncrypted));
_controllerContent.QuoteService.Value.CancelPendingQuoteRequest(requestId);
return RedirectToAction("Dashboard");
}
Any Ideas?
There's a couple things here. For one, you need to make sure that the names of the object being posted to the server match up with the Controller's parameter. For instance, if you send this Javascript object up:
{ requestIdEncrypted: "exampleString" }
or
{ requestIdEncrypted: viewModel.requestId() }
then your Controller method should accept the input.
Secondly, from your code it's not evident to me how the data is being posted. $(form).submit().error(function (messageObj) is a little confusing: is this line responsible for submitting the form? Is it a function that would be called if the form submission is unsuccessful? Is it working? It's not clear to me what you're trying to do with this. You may have to figure out another way to attach an error handler to the form, if this is what you're trying to do - unless it's working alright.
I want to route from one page to another page.
Candidatesvas- controller
Here I have code,
[Authorize, HttpPost,HandleErrorWithAjaxFilter]
public ActionResult Details(FormCollection collection)
{
Order order = _repository.GetOrder(LoggedInOrder.Id);
order.CreateDate = DateTime.Now;
order.Amount = 360;
order.Validity = 60;
_repository.Save();
}
when I click Index page,"any link", it saves in db and go to next details page.
Index.aspx:
<%:Html.actionlink("Details","Details","Candidatesvas")%>
like that...
Global.ascx:
routes.MapRouteLowercase(
"SaveVas",
"details/candidatesvas",
new { controller = "Candidatesvas", action = "Details" }
);
But when i click link, it shows "resource cannot be found". i changed many way. please help me. i can't find out the issue?
Your controller action is decorated with the [HttPost] attribute which means that this action is only accessible with the POST verb. Then you have shown some link on your page:
<%:Html.ActionLink("Details","Details","Candidatesvas")%>
But as you know a link sends GET request. If you want to be able to invoke this action you should either remove the [HttpPost] attribute from it or use an HTML <form> instead of a link.
try this
<%:Html.actionlink("Details","Details","Candidatesvas",null,null)%>
You can't use Html.actionlink for post method.
go for jquery
Or call form submit on click function.
How do I block access to any page in cakePHP. With page, I'm referring to actual views lying in the Page folder.
When I remove this line in, it works, but it also stops users from logging in. It would create a direct loop:
$this->Auth->allow('display');
Basically, when a user wants to view any page, and they are not logged in, they will be redirected to the login (app/users/login) page. After they've logged in, they will be directed to the page they last tried to access.
How would I go about this?
The problem in your situation is that all pages shown by the pagesController are the same action (display()), only using a different parameter (the page to display). You can therefore not block access to the display action, because that will block access to all pages.
If the number of pages is limited, then the easiest way to implement this is ControllerAuthorize. Read the documentation here; Using ControllerAuthorize
class AppController extends Controller {
public $components = array(
'Auth' => array('authorize' => 'Controller'),
);
public function isAuthorized($user = null) {
// Make all actions public
return true;
}
}
Then, inside your pages controller;
class PagesController extends AppController {
public function isAuthorized($user = null) {
if ('display' !== $this->request->action) {
// other actions; let he AppController handle access
return parent::isAuthorized($user);
}
if (!empty($user)) {
// Logged-in users have access to any page
return true;
}
$page = empty($this->request->params['pass'][0]) ? null : $this->request->params['pass'][0];
switch($page) {
case 'home':
case 'about':
// etc
return true;
}
// all other pages are 'private'
return false;
}
}
Just an example, of course, modify to fit your needs
use
$this->Auth->allow('\','display');
it allow all after '\' pages..
or
if you not allow except display page you do nothing.