What is the best way to POST to another website (so that the users browser is redirected as well) in MVC2? I dont want the user to be able to see the form data simply by using "view source", so I'm not keen on
<form action="http://other-site.com/action">
<%= Html.TextBox("something-secret") %>
</form>
Is it instead possible (or advisable) to use an controller action? eg
public ActionResult PostTheData()
{
return Post("http://other-site.com/action", "something-secret");
}
You can use this persons code: http://geekswithblogs.net/rakker/archive/2006/04/21/76044.aspx
to post data to the other server. And after that use Redirect(url) to redirect to the page as well.
Related
I have to call post action method in controller. my razor view is
#using (Html.BeginForm())
{
#Html.TextBox("count")
// here i have many controls
<input type="submit" value="SUBMIT"/>
}
[HttpPost]
public ActionResult Update(string count)
{
// i will do many business related actions
return View();
}
My question is, can i use normal post call by clicking submit button or shall i use ajax post method?
Which method of calling is good in mvc3 and why?
You must know the difference between Normal post and ajax
if you dont want to refresh your page then use ajax i.e you have to update content dynamically and stay on that page , otherwise use normal post
Both methods are fully supported, and the choice depends on your scenario.
You may use Ajax post method if you don't want to reload all your page on submit. Ajax allows you to make some partial refresh, so you refresh only a part of your page WHILE the normal post call loads an entire page(including headers and some other content that may not be changing).
garethb has given an answer in this link http://www.codeproject.com/Articles/429164/Html-BeginForm-vs-Ajax-BeginForm-in-MVC3.
the answer is
Ajax forms are suitable in situations, where you need to do modify or save operations asynchronously , without redirecting to any other forms.
You are using the MVC of ASP.NET, which is server-side. Posts are issued by the Javascript written on the client-side. Posts are supported by your server-side, so you can use "normal" or ajax posts as you like.
i have a list of thumbnails for the user can select one of the image.
onclick on the thumbnail open a larger image into a form.
What im trying to do now is send the id of the image selected to my controller.
Note: im using MVC 4.
how can i do that?
someone can help with this pls?
Thanks in advance:
Here is my code:
#foreach (var p in ViewBag.Images)
{
<li>
<a href="~/Files/#p.Name" onclick="swap(this); return false;">
<img src="~/Files/#p.Name"/>
</a>
</li>
}
when selected is going this img tag in my form:
<img id="main" src="" >
using this javascript for this event:
function swap(image) {
document.getElementById("main").src = image.href;
}
what i have to do now?
i trying with <input type="hidden" name="Img_Id" value="Viewbag.??????"/>
to pass this value to my controller??
First, some terminology help: You can't pass a value from the view to the controller action, the view is rendered after the controller action completes.
What you want to do is pass data from the client (web browser) to a controller action, using form fields.
In your javascript swap method, you could set the value of the Img_Id field to be the value for the selected image. When the form is submitted, the Img_Id will be posted as form data, and can be accepted as a parameter in the action.
You can use JQuery (or something else) to perform the client side actions.
Here's an example (not tested though!):
First add the ID as a data attribute on the element:
<a href="~/Files/#p.Name" data-id="#p.ID" onclick="swap(this); return false;">
Then some javascript to save that to form (using jquery here):
function swap(image) {
document.getElementById("main").src = image.href;
$("input[name='Img_Id']").val($(image).data("id"));
}
To pass a value back to your controller, you either need to submit a form, or else make an AJAX request to your controller.
In the first case, you'd need to update the value of your hidden field with javascript, and then either wait for the user to submit the form, or trigger a submit through javascript depending on what your needs are.
If you want to do an ajax request, it would be more or less the same thing, but you don't need a hidden field to store the value.
You could use jQuery in your swap function. See here for the official documentation.
If you chose to use this approach, and assuming you place your JavaScript in a separate file, then make sure you get the path for the action and controller and pass that in too.
var url = #Url.Action("Index","Home");
Therefore you may call: onclick="swap(this.id, url)"
I am new to Stack Overflow and to ASP.NET MVC.
I have been asked to do a project where I want to use ASP.NET MVC, but I have some problems wrapping my head around it and I hope some of you could get me in the right direction.
The project is a kind of a search portal. On every page there is a dropdown box where you basicly select the dataset (it's based on books) you want to search. In the dropdown is the name of the book you want to search.
Of course there is also a search field. These 2 objects are on every page and has the same function on all pages, and I can't get these 2 objects to communicate.
I have these 2 in separate partialviews and want to generate an action for the search formfield something like this:
domain.com/{bookname}/search/{searchterm}: this is the thing created from the dropdown and the search box.
But can I do this in the searchfields partialview in some way, or do I have to grab these value in each controller?
I hope this makes any sense.
Create a partial view with your dropdown and textbox, using the BeginForm helper:
<% using(Html.BeginForm("Index", "Search")) %>
<% { %>
<%= Html.DropDownList("BookNames") %>
<%= Html.TextBox("SearchTerm") %>
<% } %>
Then in your SearchController's Index action, you should be able to grab the values from the form collection or using your dropdown's and textbox's id.
public ActionResult Index(FormCollection frmCollection)
{
// ...
// also you can redirect to another action/controller if you needed
// return RedirectToAction("...", "...");
}
or
public ActionResult Index(int bookNames, string searchTerm)
{
// ...
// also you can redirect to another action/controller if you needed
// return RedirectToAction("...", "...");
}
Hope this helps.
I have implemented a similar search requirement on one of my projects with ASP.NET MVC. However, I had the Dropdown and Search in a single Partial View.
I then used Javascript to pick up the selected values and redirect the user to the URL with pattern domain.com/{criteria}/search/{searchWord}.
This way I had to use a single controller to search, with a single view to view search results.
I am having a hard time figuring out how to redirect to an outside source.
in my code, I have
<%= Html.ActionLink("New Name Search", "Index") %>
which will allow me to navigate within the code.
how do I redirect to ...google for example?
google
The purpose of the ActionLink helper is to generate links that will direct the user to a controller action that you've defined.
If you want to navigate the user to an outside source you should just use a regular anchor tag.
Response.Redirect("http://google.com/");
(It's not really MVC-specific, by the way)
If you are redirecting from your controller (or action filter, etc.) you can use the RedirectResult as your ActionResult type:
RedirectResult("http://www.google.com");
This is essentially doing a Response.Redirect, but is the preferred way of sticking with ASP.NET MVC conventions.
If you are just creating a link inside a View, just use Click to go to Google.
"Redirecting" means many things.
If you just want to show a link that redirects the user to another URL you can use the anchor tag normally in you templates.
google
Now, if you want to redirect the user within the controller, call the Redirect Method.
Result("http://www.google.com");
In my master page, I have a language dropdown menu. When the user selects a language from the dropdown, a submit sends the currently selected language to the "Translate" method in my controller. After which it should redirect to the url it was before the translation submit so it can show the exact same page, but now in the newly selected language.
How would I best go about this? Should I send the current url somehow in a hidden field? Or can I maybe send the current routevaluedictionary, so my translate method can redirectToRoute directly?
Or maybe there is an entirely better way?
--EDIT--
Because I want my bookmarks to include the site language too, all my exposed actions have a siteLanguage parameter too. If I could somehow efficiently show the user a bunch of regular (GET) links where the siteLanguage parameter is filled in with the relevant value, that would be even better. But as far as I know, there is no way to put links in a dropdown except with java maybe.
I have a similar situation, and I solved it slightly differently.
Because my master page had functionality in it, I created a new base controller class (that inherits from Controller) and all my real controllers inherit from my custom class.
Then, I implement OnActionExecuting in the base class to do some common work.
Then, in your masterpage, if you have a form like this, it will submit to the current URL with a GET request and add the language as a querystring parameter:
<form id="language" method="get" >
<select name="language">
<option value="en">English</option>
<option value="es">Spanish</option>
...
</select>
</form>
Use jQuery to wire it up to autosubmit, etc.
You could look for a language parameter in the querystring in your base controller class and set the flag that tells the real controller method which language to use. In the model, you directly go to the real controller to regenerate the page and avoid a redirect.
Note, this only works universally, if you are not already using querystring parameters.
If this doesn't work for you, you could also use your current method, but include the URL to be redirected to in a hidden field like this:
<%= Html.Hidden("redirect", Request.Url %>
public ActionResult Translate(string _lang){
switch(_lang){
case "English":
return View("English");
case: "French":
return View("French");
default:
return View("English");
}
I would personally do it like this
I would put the return url in the querystring, just like forms authentication does when it redirects to the login page.
Include the returnUrl in the routeValues when you do your Translate request.