I'm attempting to wrap my head around .NET MVC5 routing.
I've got a form:
#using (Html.BeginForm("ProductsCheaperThan", "Home", FormMethod.Post))
{
<input type="text" name="comparisonPrice" />
<button type="submit">Search!</button>
}
And I've got a controller Home and an action ProductsCheaperThan which takes a parameter comparisonPrice
public ActionResult ProductsCheaperThan(decimal comparisonPrice)
{
ViewBag.FilterPrice = comparisonPrice;
var resultSet = new ProductService().GetProductsCheaperThan(comparisonPrice);
return View(resultSet);
}
This posts the value in the input (let's suppose that the value I'm posting is 20) back to my action, and correctly routes me to ~/Home/ProductsCheaperThan. The problem is, I'd like to be routed to ~/Home/ProductsCheaperThan/20
I'd like to do this so that if somebody bookmarks the page they don't end up getting an error when they revisit the page.
I thought that adding something like:
routes.MapRoute(
name: "ProductsCheaperThan",
url: "Home/ProductsCheaperThan/{comparisonPrice}",
defaults: new { controller = "Home", action = "ProductsCheaperThan", comparisonPrice = 20 }
);
might work, and I have one solution to my problem which changes the form to a GET
#using (Html.BeginForm("ProductsCheaperThan", "Home", FormMethod.Get))
and produces a URL of ~/Home/ProductsCheaperThan?comparisonPrice=20, but that uses a query string instead, and isn't exactly what I was aiming for.
Can anybody help me get my URL right?
You should add [HttpPost] attribute to your action
[HttpPost]
public ActionResult ProductsCheaperThan(decimal comparisonPrice)
{
ViewBag.FilterPrice = comparisonPrice;
var resultSet = new ProductService().GetProductsCheaperThan(comparisonPrice);
return View(resultSet);
}
One option is to use JQuery -
<div>
<input type="text" name="comparisonPrice" id="comparisonPrice" />
<button type="button" id="Search">Search!</button>
</div>
#section scripts{
<script>
$(function () {
$("#Search").click(function () {
window.location = "#Url.Action("PriceToCompare", "Home")" + "/" + $("#comparisonPrice").val();
});
});
</script>
}
Above script will result in - http://localhost:1655/PriceToCompare/Home/123
I think you can specify your route values using an overload:
#using (Html.BeginForm("Login", "Account", new { comparisonPrice= "20" }))
{
...
}
Related
I am trying to mess around with a foreign API, but i cant seem to get the last bit working. I have succesfully retrieved data from the API but i cannot get the search functionality to cooperate with the API. The search function needs an id, and from that an object is returned. Here is what i have so far:
Controller
[HttpGet]
public ActionResult GetCardsByID(string idNumber)
{
//idNumber = "c353618d9f76c03a0c7d549f2d877f9533112d0c";
ViewBag.Message = "Your GetCardsByID page.";
var client = new RestClient("https://api.magicthegathering.io");
var request = new RestRequest("v1/cards/{id}", Method.GET);
request.AddUrlSegment("id", idNumber);
IRestResponse response = client.Execute(request);
var content = response.Content;
CardContainer cards = JsonConvert.DeserializeObject<CardContainer>(content);
return View();
}
View
#{
ViewBag.Title = "GetCardsByID";
}
<h2>#ViewBag.Title.</h2>
<h3>#ViewBag.Message</h3>
<p>
In this tab we can retrieve a card by searching with an id
</p>
<form method="get" action="#Url.Action("GetCardsByID", "CardsController")">
<label for="idNumber">Search</label>
<input type="text" name="idNumber" id="idNumber" />
<button type="submit">Perform search</button>
</form>
Routing
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id =
UrlParameter.Optional }
);
}
}
The error i get is as follows: (Translated)
Server error in program'/'.
Resource not found.
Description: HTTP 404, the resource you are looking for, or one of its dependencies, has possible been removed and is not accesable at this moment. Check if the following URL-addresses are spelled correctly
Wanted URL address: /CardsController/GetCardsByID
Home page GetCardsByID page Search error
When you call a controller, don't put controller on the end of it.
Try this:
<form method="get" action="#Url.Action("GetCardsByID", "Cards")">
<label for="idNumber">Search</label>
<input type="text" name="idNumber" id="idNumber" />
<button type="submit">Perform search</button>
</form>
Note the Url.Action is Cards, not CardsController
Even though your controller is called CardsController.cs
You know you could probably do this easier entirely in javascript
In View-
<input type="text" hidden="hidden" id= "nameString" name="nameString" value="xyz" />
#Html.ActionLink("Save", "HomePage", "ControllerName", new { nameString = "/* Value from above input here.*/" })
In this case I want to pass "xyz" through this hyperlink.
Can't use Get or POST methods to pass this value.
This HomePage view is not the current ActionLink view either, so can't grab the value in Controller using Request.Form["nameString"];
I tried JQuery like following but its not working-
var nameVar = document.getElementById('nameString').value;
$.ajax({
type: 'GET',
url: "#Url.Action("HomePage", "ControllerName")",
data: { nameString : nameVar }
});
I checked in debugger and saw that Controller is actually getting value and processing it but nothing is coming on browser. I am not sure how ajax works.
since the redirect will happen in jquery you don't need a helper
<input type="button" class="btnRedirect" value="Click Here" />
then in your script
$('.btnRedirect').on('click', function(){
var url = '#Url.Action("HomePage", "ControllerName", new { textValue = "----" })'.replace("----", $('#nameString').val());
window.location = url;
});
This is how it would be done with ajax although this will not handle returning a page.
var name = $("#nameString").value;
$.get("/ControllerName/HomePage",{ nameString : nameVar })
.done(function(){
window.location.assign("/ControllerName/HomePage");
});
I have the following in my view:
#Html.DropDownList("ProductionOrder", null, htmlAttributes: new { #class = "form-control", #id = "ProductionOrder" })
<div class="col-lg-6" id="ProductionOrderDetails"></div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
$(function () {
$("#ProductionOrder").change(function () {
var po = $("#ProductionOrder").val().toString();
//This alert is for debug purpose only
alert(po);
$.get('/wetWashRequests/GetDetails/' + po, function (data) {
$('#ProductionOrderDetails').html(data);
$('#ProductionOrderDetails').fadeIn('fast');
});
})
})
</script>
then I have the following in my controller:
public PartialViewResult GetDetails(string PONumber)
{
var details = db.vwProductionOrderLookups.Where(x => x.No_ == PONumber).SingleOrDefault();
return PartialView("_ProductionOrderDetails", details);
}
What I don't understand is why it doesn't pass the value to the controller or why, when I enter the URL manually in the browser, like so(http://localhost:51702/wetWashRequests/GetDetails/WO033960), it also doesn't assign it to the parameter and so returns no data.
What am I missing? I thought I was on the right track but...
You need to edit the route configuration to allow URL of type {controller}/{action}/{PONumber}. Otherwise, you can also send the PONumber via querystring, so that your URL looks like this:
http://localhost:51702/wetWashRequests/GetDetails?PONumber=WO033960
Use URL.Action() method
var url= "#Url.Action("wetWashRequests","GetDetails")"+"?PONumber="+po;
$.get(url,function(data)
{
});
I think this modification will work:
$.get('/wetWashRequests/GetDetails?PONumber=' + po,
please note #malkam's remark to always use: #Url.Action(controller,action)
var url= "#Url.Action("wetWashRequests","GetDetails")"+"?PONumber="+po;
To clarify:
In your app-start you'll probably have the default routing:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index",
id = UrlParameter.Optional }
);
This means you can have URL's like:
/wetWashRequests/GetDetails/999
but then the 999 is bound to a parameter is called id.
For all other variables you'll need the
/wetWashRequests/GetDetails?someParameter=999
syntax.
Alternatively, you can modify your routing.
I have the default route defined as
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
I want to generate a url like this in html -
<form action="/app/Request/c35d4520-ba0b-452f-837b-a00046f40647 method="post">
But if I code Razor page like -
#using (Html.BeginForm("Default", "Request", FormMethod.Post, new { id = ViewBag.AppId }))
The rendered html is -
<form action="/app/Request" id="c35d4520-ba0b-452f-837b-a00046f40647" method="post">
How can I force the razor generate the url as controller/action/ID format?
Thanks
Try using Html.BeginRouteForm
#using (Html.BeginRouteForm("Default", new { controller = "foo", action = "bar" }, FormMethod.Post, new { id="foo", enctype="multipart/form-data", accept_charset="utf-8" }))
{
}
the problem is in the way you arrange the parameters in the Html.BeginForm call. According to the parameters you feed in, you are currently calling
Html.BeginForm(actionName, controllerName, formMethod, htmlAttributes)
Therefore, new { id = ViewBag.AppId } is treated as htmlAttributes. And that is why the id is renedered as an attribute in the form tag.
Instead, you should swap the positions of method and the id as below
Html.BeginForm("Default", "Request", new { id = ViewBag.AppId }, FormMethod.Post))
Let me know if it works for you :)
The problem has been solved by a function -
protected ActionResult RedirectToAppAction(string controllerName)
{
return this.Redirect(string.Format("/App/{0}/{1}", controllerName, this.Id));
}
I am trying to get my product search URL to look like "Products/Search/{search term here}".
I am using attribute based routing and my controller action looks like this:
[HttpGet]
[Route("Products/Search/{searchTerm?}", Name="ProductSearch")]
public ActionResult Search(string searchTerm = "")
{
return View();
}
I have tried using the HTML Helper for BeginForm and BeginRouteForm (shown below) but have not had luck with either. The right action is being called, but my URL looks like "Products/Search?searchTerm"
BeginRouteForm
#using (Html.BeginRouteForm("ProductSearch", new { searchTerm = "" }, FormMethod.Get, new { Class = "navbar-form navbar-right", role = "search" }))
{
<div class="form-group">
#Html.TextBox("searchTerm", null, new { Class = "form-control", placeholder = "Item # or Name" })
</div>
<button type="submit" class="btn btn-default">Search</button>
}
BeginForm
#using (Html.BeginForm("Search", "Products", new { searchTerm = "" }, FormMethod.Get, new { Class = "navbar-form navbar-right", role = "search" }))
{
<div class="form-group">
#Html.TextBox("searchTerm", null, new { Class = "form-control", placeholder = "Item # or Name" })
</div>
<button type="submit" class="btn btn-default">Search</button>
}
I have gone through debugging and the right route is selected, the URL is just not displaying how I wanted it to. What am I missing?
Here is the solution I suggest -
You have the following controller Action -
[HttpGet]
public ActionResult Search(string searchTerm = "")
{
return View();
}
Let the view be -
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
$(function () {
$('#click').click(function (e) {
var name = $("#search").val();
var url = '#Url.Action("Search", "Action")' + '/' + name;
window.location.href = url;
});
});
</script>
<input type="text" name="searchText" id="search"/>
<input type="button" value="click" id="click"/>
And when you click the button -
Do not forget to have proper route to be added on to the route configuration -
routes.MapRoute(
name: "searchaction",
url: "{controller}/{action}/{searchTerm}",
defaults: new { controller = "Action", action = "Search" }
);
The problem you think you are experiencing isn't because of anything about ASP.Net MVC. All Html Forms that use the method GET will translate all input elements into QueryString parameters. This is just a W3C standard.
If you want this to work, you'll have to write jQuery to throw an event before the form is submitted, take the text value from the input store it temporarily, empty the input box, and then update the action by appending the temporary value.
I don't think that BeginRouteForm works the way that you're expecting it to. According to the documentation, all that the method does is insert a <form> using the arguments provided. If you had provided something other than an empty string for the route value such as , new { searchTerm = "somesearchterm" }, you would see that show up in the Url as "/product/search/somesearchterm". As it is now, however, the form will be processed as normal, putting the search term on the Url as a normal query parameter.