I have:
public ActionResult Create(Guid appId)
{
var vm = new CreateViewModel(appId);
return View(vm);
}
[HttpPost]
public ActionResult Create(CreateViewModel vm)
{
// this does some stuff
}
Now, in the View I use this for creating the Form:
#using(Html.BeginForm())
{
}
Standard.
How ever, it produces the wrong HTML:
<form action="/SomeController/Create?appId=414FDS-45F2SF-TEF234">
This is not what I want posted back, I don't want appId what so ever. Just the Create
How do you get around this?
You can use another overload of Html.BeginForm to explicitly specify the action you want:
#using(Html.BeginForm("Create", "SomeController"))
{
}
This will not append anything to the URL by default.
Related
I have the following Get action:
[HttpGet]
[AllowAnonymous, OnlyAnonymous]
public ActionResult VerifyVoucherCode()
{
var model = new VerifyVoucherCodeModel();
model.VoucherCode = Request.GetFirstQueryValue("token", "voucherCode");
if (!string.IsNullOrEmpty(model.VoucherCode))
{
// prepopulates the code for the user already in a form
}
return View(model);
}
And a POST action for when the user submits the form:
[HttpPost, AjaxOnly]
[AllowAnonymous, OnlyAnonymous]
[ValidateAntiForgeryToken]
public ActionResult VerifyVoucherCode(VerifyVoucherCode model)
{
return ExecuteAction(() =>
{
// do something
});
}
I want to change the logic so that if the voucher code is in the request, the form is submitted automatically for the user without them having to submit the form themselves (i.e. it takes them straight to that Post action). I've looked at lots of Stack Overflow questions and answers and it seems I cannot redirect to the Post action. I have also tried creating a second Get action which calls the Post action but that does not work either. Does anyone know the correct way to approach this problem?
Thanks
Assuming the model contains a single string for the voucher, you can do something like this:
[HttpGet]
[AllowAnonymous, OnlyAnonymous]
public ActionResult VerifyVoucherCode(string id)
{
if(string.IsNullOrEmpty(id))
{
MyModel model1 = new MyModel();
...
return View(model1);
}
//otherwise process the voucher here
...
return RedirectToAction("SuccessMsg");
}
If I have a controller action to redirect to another action like so:
public ActionResult Index()
{
RedirectToAction("Redirected", "Auth", new { data = "test" });
}
public ActionResult Redirected(string data = "")
{
return View();
}
The URL bar will have something like "Redirected?data=test" in it, which AFAIK is the proper behavior. Is there a way I can pass a variable directly to the Redirected ActionResult without a change on the client?
I'd like to pass "test" directly to the Redirected ActionResult without the URL changing. I'm sure there's a simple way to do this, but it is escaping me.
I know I can make a static variable outside the functions that I can pass the variable to and from, but that doesn't seem like a proper solution.
You can use TempData variable.
public ActionResult Index()
{
TempData["AfterRedirectVar"] = "Something";
RedirectToAction("Redirected", "Auth", new { data = "test" });
}
public ActionResult Redirected(string data = "")
{
string tempVar = TempData["AfterRedirectVar"] as string;
return View();
}
This link could be helpful.
Yes, use TempData.
public ActionResult Index()
{
TempData["data"] = "test";
RedirectToAction("Redirected", "Auth"});
}
public ActionResult Redirected()
{
var data = TempData["data"].ToString();
return View();
}
I hope this could help you:
https://stackoverflow.com/a/11209320/7424707
In my opinion TempData isn't the most proper solution. If I were you I would look for another solution.
Otherwise, do you really need to call RedirectToAction (to call an action from another controller)? Or are your actions in the same controller for instance?
I have an model class that is used to validate some user input.
I have an controller with the following.
public IActionResult Checkout(GiftCard giftCard)
{
}
I was wondering how I could on an different action redirect it back to it such as
public IActionResult Preview(GiftCard giftCard)
{
return RedirectToAction("Checkout");
}
The above doesn't work because asp.net is trying to find an action without the model like the one below
public IActionResult Checkout()
{
}
if your url going to be really long make a shorturl so load that and redirect from there, if you use ajax it wont be visible
You could use action with another name and apply action selector to your renamed method. Like next:
[ActionName("Checkout")]
[HttpPost] //Recomend you send user input via post
[ValidateAntiForgeryToken] // and use validation token
public IActionResult CheckoutConfirmed(GiftCard giftCard)
{
//your code
}
public IActionResult Checkout()
{
//your code
}
Check out more about ASP.NET MVC - Selectors
If you need more information about ValidateAntiForgeryToken, you could find it there - Chapter 12: Security
And also, you could find great article about posting there - ASP.NET MVC Preview 5 and Form Posting Scenarios
RedirectToAction has a second parameter called routeValues with which you can pass the GiftCard like following.
public IActionResult Preview(GiftCard giftCard)
{
return RedirectToAction("Checkout", giftCard);
}
I am trying to pass a textbox's text back to a method in my controller, but the data is not being passed in the parameter
I am genuinely confused, i'm following another example but i'm getting a different result, ie - my method behaving as if no parameter is passed
Code
public ActionResult Index(string searchString)
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
var listOfAnimals = db.Animals.ToList();
if (!String.IsNullOrEmpty(searchString))
{
listOfAnimals = listOfAnimals.Where(a => a.AnimalName.ToLower().Contains(searchString.ToLower())).ToList();
}
return View(listOfAnimals);
}
and here is my razor form from my view page
#using(Html.BeginForm("Index", "Home"))
{
#Html.TextBox("searchString")
<input type="submit" id="Index" value="Index" />
}
Can anybody spot why this isn't working?
If more code is needed, please let me know but i think the issue is isolated to here
You code is correct.
Since you didn't add [HttpGet] or [HttpPost] before index method.
This method was called twice.
The first call ran when producing the page with form via url http://server/Home/Index. This call was an http get and searchString mapped from URL was null, which is correct.
The second call ran when you clicked submit button. Correct value would be mapped by MVC correctly.
You need to have 2 Index actions (two methods), one without decorations (GET verb) and another one decorated with HttpPost (POST verb). Basically, when you go to the index page, the GET action is executed. When you submit the form, a POST request is executed and the Index decorated with HttpPost is executed.
// GET
public ActionResult Index() { ... }
// POST
[HttpPost]
public ActionResult Index(string searchString) { ... }
Francisco Goldenstein wrote the recommended way. It means you can have two Index() actions:
// for GET
public ActionResult Index() { ... }
// for POST
[HttpPost]
public ActionResult Index(string searchString) { ... }
However it is possible to have one Index() method for handling both (GET and POST) requests:
public ActionResult Index(string searchString = "")
{
if(!string.IsNullOrEmpty(searchString)
{ /* apply filter rule here */ }
}
You wrote, your code is not working. Do you mean, your action method is not requested after click on the button? Consider to allow empty value Index(string searchString = "")
If your action method is fired but variable is empty, check the name on the View() side. Textbox must not be disabled, of course.
I am new to Asp.net MVC and have no idea as to how can i perform the search. Here's my requirement, please tell me how will you handle this :-
I need to have textbox where user can enter a search query or string. The user then clicks on a button or presses enter to submit it. The string needs to matched with a table's property name.
NOTE:- Querying the data and fetching the result isn't the main point here. All I need to know is how will you take the user input and pass it to a controller action or whatever for further processing. Just tell me how will you read the user input and where will you send it to search.
Asp.Net MVC uses standard HTTP verbs. For the html part, it's a normal html form that points to an url. Server side, that url will be routed to a controller/action which will handle the input and do what is needed.
Let's have a sample. You want to make a search form. First of all, it's a best practice to have search forms use the HTTP GET method instead of POST, so the search results can be bookmarked, linked, indexed, etc. I won't be using Html.BeginForm helper method to make things more clear.
<form method="get" action="#Url.Action("MyAction", "MyController")">
<label for="search">Search</label>
<input type="text" name="search" id="search" />
<button type="submit">Perform search</button>
</form>
That's all the html you need. Now you'll have a controller called "MyController" and the method will be something like this:
[HttpGet]
public ActionResult MyAction(string search)
{
//do whatever you need with the parameter,
//like using it as parameter in Linq to Entities or Linq to Sql, etc.
//Suppose your search result will be put in variable "result".
ViewData.Model = result;
return View();
}
Now the view called "MyAction" will be rendered, and the Model of that view will be your "result". Then you'll display it as you wish.
As always in an ASP.NET MVC application you start by defining a view model which will express the structure and requirements of your view. So far you have talked about a form containing a search input:
public class SearchViewModel
{
[DisplayName("search query *")]
[Required]
public string Query { get; set; }
}
then you write a controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new SearchViewModel());
}
[HttpPost]
public ActionResult Index(SearchViewModel model)
{
if (!ModelState.IsValid)
{
// There was a validation error => redisplay the view so
// that the user can fix it
return View(model);
}
// At this stage we know that the model is valid. The model.Query
// property will contain whatever value the user entered in the input
// So here you could search your datastore and return the results
// You haven't explained under what form you need the results so
// depending on that you could add other property to the view model
// which will store those results and populate it here
return View(model);
}
}
and finally a view:
#model SearchViewModel
#using (Html.BeginForm())
{
#Html.LabelFor(x => x.Query)
#Html.EditorFor(x => x.Query)
#Html.ValidationMessageFor(x => x.Query)
<button type="submit">Search</button>
}
This is the best way to do it.
Create a ViewModel
public class SearchViewModel
{
public string Query { get; set; }
}
Create a Controller
public class SearchController : Controller
{
[HttpPost]
public ActionResult Search(SearchViewModel model)
{
// perform search based on model.Query
// return a View with your Data.
}
}
Create the View
// in your view
#using (Html.BeginForm("Search", "SearchController"))
{
#Html.TextBox("Query")
<input type="submit" value="search" />
}
hope this helps