Passing value from View to Controller, what am I missing? - c#

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.

Related

How to pass a parameter to a controller from a cshtml view in mvc

net MVC 4 I followed the microsoft tutorials on how to pass a parameter to a controller from a cshtml view in mvc and I keep getting an error that says the resource cannot be found.If I put a break point in the cshtml I can actually see the value of the Id but it is not hitting the controller at all seems like it cant find it
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name
changed, or is temporarily unavailable. Please review the following
URL and make sure that it is spelled correctly.
Requested URL: /UploadLogs/DisplayUploadedFileContents/89
This is my controller method
public class DisplayUploadedFileController : Controller
{
private MarketingDBEntitiesModel db = new MarketingDBEntitiesModel();
// GET: DisplayUploadedFile
public ActionResult DisplayUploadedFileContents(int UploadId)
{
return View(db.marketingdbclients_dataTable.OrderByDescending(r => r.ClientId).Where(r => r.ClientDataId < 1000).ToList());
// return View();.
}
}
My line in the cshtml
<td>
#*#Html.ActionLink("Details", "Details", new { id = item.UploadId })*#
#Html.ActionLink("Details", "DisplayUploadedFileContents", new { id = item.UploadId })
</td>
My route config
routes.MapRoute(
name: "DisplayUploadedFileContents",
//url: "",
url: "{controller}/{action}/{id}",
defaults: new { controller = "DisplayUploadedFile", action = "DisplayUploadedFileContents", id = UrlParameter.Optional });
Making a couple of changes should get this working.
First, if you want to use the routing for the url like this {controller}/{action}/{id}, change the parameter name in the controller action from UploadId to id:
public ActionResult DisplayUploadedFileContents(int id)
Next, it looks like you're linking from a different controller since in the error the requested URL is /UploadLogs/DisplayUploadedFileContents/89 (note UploadLogs is not DisplayUploadedFile).
Linking to the DisplayUploadedFile controller from a view that belongs to a different controller, you will need to use this overload taking 5 parameters:
#Html.ActionLink("Display File Contents", "DisplayUploadedFileContents", "DisplayUploadedFile",
null, new { id = item.UploadId })
However, if you're accessing the controller from a view within the same controller you should be able to use this overload for ActionLink taking 3 parameters:
#Html.ActionLink("Display File Contents", "DisplayUploadedFileContents", new { id = item.UploadId })
Please refer to the ActionLink documentation
url: "{controller}/{action}/{id}
You didn't respect the routing configuration. Try:
#Html.ActionLink("DisplayUploadedFile", "DisplayUploadedFileContents", new { UploadId = item.UploadId })
#Html.ActionLink("DisplayUploadedFile", "DisplayUploadedFileContents", new { UploadId = item.UploadId}, null )
You need the Argument for "htmlArgument"
Please Refer to: HTML.ActionLink method

Routing without query string

I have added the following route before the default route
routes.MapRoute(
name: "RecordDefault",
url: "{controller}/{action}/{name}",
defaults: new { controller = "Person", action = "Record" }
);
I can hit the page I want using: sitename/Person/Record/John
But I have an global search in the navigation with the following code
#using (Html.BeginForm("Record", "Person", FormMethod.Get, new { #class = "navbar-form navbar-left" }))
{
#Html.TextBox("name", "", new { #class = "form-control", placeholder = "Search Name" })
}
When I submit the form the following URL is displayed: sitename/Person/Record?name=John
What do I have to do to ensure the URL is formatted without the query string parameter?
Thanks
Not the same as the posted duplicate, that marked answer does not resolve my problem and according to the comments it also didnt work for others.
Your form generates ../Person/Record?name=John because a browser has no knowledge of your routes (which is c# code running on your server). And the HTML standards require that the value of successful form controls be added as query string values when the method is GET.
In order to generate your preferred url (../Person/Record/John), you need javascript to intercept and cancel the default submit, and build a url to navigate to. Using jQuery:
$('form').submit(function() {
var baseUrl = $(this).attr('action');
// or var baseUrl = '#Url.Action("Record", "Person")';
var url = baseUrl + '/' + $('#name').val();
location.href = url; // redirect
return false; // cancel the default submit
});
Use form post FormMethod.Post instead of Get. So the value will be not appeared in querystring.
#using (Html.BeginForm("Record", "Person", FormMethod.Post, new { #class = "navbar-form navbar-left" }))
{
#Html.TextBox("name", "", new { #class = "form-control", placeholder = "Search Name" })
}
In your Controller add the following -
[HttpPost]
public ActionResult Record(string name)
{
//code for what needs to be performed.
return View();
}
In your view add the following code replacing your existing and check -
#using (Html.BeginForm("Record", "Person", FormMethod.Post))
{
#Html.TextBox("name")
<input type="submit" />
}

MVC route URL not containing parameter

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" }))
{
...
}

ASP.NET MVC 4 to Generate URL Formate as Controller/Action/ID

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));
}

Routing Issue on asp.net mvc 5 GET

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.

Categories

Resources