I have a text box that I enter data into and pass with a post to my 'home' controller on action 'results'
I want the url to end up looking like this when I post back
https://localhost:44301/Home/Results/San Francisco, CA, United States
I'm passing the text box data like this.
#using (Html.BeginForm("Results", "Home", FormMethod.Get, new { #class = "navbar-form navbar-left", role = "search" }))
{
<div class="form-group">
<input type="text" class="form-control" placeholder="Search" id="navbarautocomplete" name="location">
<button type="submit" class="btn btn-default">Submit</button>
</div>
}
Here is my routing.
routes.MapRoute("SearchResults",
"home/results/{location}",
new { controller = "Home", action = "Results", location = ""}
);
How do I set my routing or my form to see the data that has been submitted as location in my url?
I can get it to look like this.
https://localhost:44301/home/results?location=San+Francisco%2C+CA%2C+United+States
but I want san francisco after /results/
As #StephenMuecke mentions in the comments, you could POST your search value to a (separate) action, then redirect to your results page, passing the location as a parameter:
#using (Html.BeginForm("Search", "Home", FormMethod.Post, new { #class = "navbar-form navbar-left", role = "search" }))
{
<div class="form-group">
<input type="text" class="form-control" placeholder="Search" id="navbarautocomplete" name="location">
<button type="submit" class="btn btn-default">Submit</button>
</div>
}
Then in your controller:
[HttpPost]
public ActionResult Search(string location)
{
return RedirectToAction("Results", new { location = location });
}
public ActionResult Results(string location)
{
return Content("location is: " + location);
}
You'll also to have the following route set up in your RouteConfig to get the friendly URL (make sure this is above the default route, as they match top-down).
routes.MapRoute(
name: "SearchResults",
url: "Home/Results/{location}",
defaults: new { controller = "Home", action = "Results" }
);
Related
I have this form shown below:
#using (Html.BeginForm("NewArticle", "News",FormMethod.Post, new { name = "createForm", onsubmit = "return validateFormNewPost()" }))
{
#Html.TextBoxFor(p => p.adName, new { #class="form-control"})
#Html.DropDownListFor(p => p.tyepId, listType, "---Select Type---", new { #class = "form-control" })
#Html.TextBox("imgPath", "", new { #class = "form-control", id = "imgPath" })
#Html.TextAreaFor(p =>p.AdDescription,new { id = "editor" })
<button class="btn btn-reset btn-dark" type="reset">Reset</button>
<button class="btn-submit btn-primary btn" type="submit">Submit</button>
}
Controller:
[HttpGet]
public ActionResult NewArticle()
{
List<AdType> listType = typeModel.listForAdd();
SelectList s = new SelectList(listType, "id", "AdType1");
ViewBag.ListType = s;
return View();
}
[HttpPost]
public ActionResult NewArticle(Advertisement model,string imgPath)
{
// my code
return RedirectToAction("Category","News");
}
I'm trying to make a POST request by submit the form but it always send form data with a GET request. How can I fix it?
How are you sending the POST? Are you using AJAX or JavaScript from your MVC page or are you trying to hit the endpoint with an external application like Telerik Fiddler or Postman?
You can not make a POST request directly from the browser using the URL, my assumption is that may be what you are doing.
#using (Html.BeginForm("MethodNameinController", "Controller Name", FormMethod.Post))
{
//form fields with submit button
<input type="submit" value="Save" class="btn btn-default" id="create" />
}
And in Controller
[HttpPost]
public ActionResult MethodNameinController(Model model)
{
}
i'm trying to get an absolute url after sending a parameters from action link and I need it to be like
http://MySite/Controller/View/CityName
so I will be able to preform a search on the results page and no losing the first parameter (e.g.)
http://MySite/Controller/View/NewYork?Lecture=bobdillen
code :
#foreach (var city in #ViewBag.City)
{
#Html.ActionLink((string)#city, "LectureIn", new { #city }, null)
}
the action(LectureIn) code :
#using (Html.BeginForm("Search", "Lecture"))
{
<div class="form-group">
<div id="searchLecture" class="input-group">
#Html.TextBoxFor(m => m.SearchTerm, new { #class = "form-control", placeholder = "" })
<span class="input-group-addon">
<button type="submit"> <i class="glyphicon glyphicon-search"></i></button>
</span>
</div>
</div>
}
and in the controller :
public ActionResult LectureIn(string search = null)
{
// Do Staf
return View();
}
I have tried to change the routes but it didn't change
routes.MapRoute(
"lectureIn",
"{controller}/{action}/{id}",
new { controller = "TMaps", action = "lectureIn", City = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Here is my form. If I remove the classdate parameter it works fine, but I can't trigger the controller if I have a date in the routevalues
using (Html.BeginForm("Results", "Home", new { classDate = DateTime.Now }, FormMethod.Get, new { #class = "navbar-form navbar-left", role = "search" }))
{
<div class="form-group">
<input type="text" class="form-control" placeholder="Search" id="navbarSearchQueryWithin" name="location">
</div>
<button type="submit" class="btn btn-default">Submit</button>
}
Here is my controller
[HttpGet]
public ActionResult Results(string navbarSearchQueryWithin, DateTime classDate)
{
var viewModel = new YogaSpaceListViewModel
{
SearchQuery = navbarSearchQueryWithin
};
return View("Search", viewModel);
}
I have a partial view:
#model MsmStore.Domain.Products
<h3 class="lead">
#Model.Name
</h3>
#* <img src="~/images/MainLogo.jpg"/>*#
<p>
#Model.Decription
</p>
<p>
#Model.Price
</p>
#using (Html.BeginForm("UpdateBasketSummary", "Order", new { ProductID = Model.ID }))
{
#Html.Hidden("ProductID", Model.ID)
<input type="button" value="Click" />
}
this Html.BeginForm does not call action method (UpdateBasketSummary).
and this is my Action Method:
[HttpPost]
public PartialViewResult UpdateBasketSummary(string ProductID)
{
// orderRepository.AddToCart(ProductID, 1);
return PartialView("BasketSummary", ProductID);
}
this is my routing code:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(null, "{controller}/{action}");
An input with type button will not submit the form, by default. You need to change it to
<input type="submit" value="click" />
or
<button type="submit" value="click" />
Input of type button will not post your form, replace it with..
<input type="submit" value="Click" />
please, try this:
#using (Html.BeginForm("UpdateBasketSummary", "Order"))
{
#Html.Hidden("ProductID", Model.ID)
<input type="button" value="Click" />
}
[HttpPost]
public PartialViewResult UpdateBasketSummary(int ProductID)
{
//orderRepository.AddToCart(ProductID, 1);
return PartialView("BasketSummary", ProductID);
}
btw. not need to send ProductID in BeginForm, as you already did it by Hidden field
Edited: due to #Ben Griffiths answer:
you also need to change type='button' to type='submit' of course
Your action requires the http method POST and yet your beginForm call does not set the http method to POST. Without explicitly setting the http method you will be forced to use the default, which is a GET.
Browse action:
public ActionResult Browse(int? Category, string sortOrder, string currentFilter, string searchString, int? page)
I could send category id using action link
#Html.ActionLink("Name", "Browse", new { sortOrder = ViewBag.NameSortParm, currentFilter=ViewBag.CurrentFilter, Category=ViewBag.CategoryId })
How to pass category id from form to controller?
<div id="search">
#using (Html.BeginForm("Browse", "Home", FormMethod.Get))
{
#Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
<input type="submit" value="Search" />
}
</div>
You can send it in a hidden input:
#using (Html.BeginForm("Browse", "Home", FormMethod.Get))
{
#Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
#Html.Hidden("Category",ViewBag.CategoryId)
<input type="submit" value="Search" />
}