sending string from view to controller in asp.net - asp.net-mvc

I'm trying to get data from view in string
Here's the code in View
And in controller, I'm getting this customer name like this:
public ActionResult ViewCustomerDetails(string c_name) {
List<Sale> customerList = new List<Sale>();
customerList = db.Sales.Where(x => x.sale_customer == c_name).ToList();
double total_cash_recieved = 0;
double total_amount = 0;
foreach (var customer in customerList) {
total_cash_recieved = total_cash_recieved + (double)customer.cash_recieved;
total_amount = total_amount = (double)customer.sale_amount;
}
double remaining_balance = total_amount - total_cash_recieved;
ViewBag.TotalAmount = total_amount;
ViewBag.TotalRecieved = total_cash_recieved;
ViewBag.TotalRemaining = remaining_balance;
return View(customerList);
}
But the problem is, in c_name variable, I'm getting null.
Anyone know how to correct it or solution?

Since your parameter name is c_name, you should include that in your querystring as Burak mentioned in his answer.
If you prefer, you can render the link using Html.ActionLink helper method.
Html.ActionLink("View","ViewCustomerDetails","Admin",new { c_name=customer.name},null)
Or if you prefer to keep the existing url you have, you can update your ViewCustomerDetails method's parameter name to Id so that with the default route definition, your unnamed parameter value will be mapped to Id parameter.
public ActionResult ViewCustomerDetails(string id) {
var c_name=id;
// your existing code
}
It is always a good idea to pass a unique Id ( Customer ID etc..) instead of passing a name to show the details because I know more than one scott in the world.

You should send it like this:
And make sure that #customer.name is not null before going to the server side.

or you can set new route to RouteConfig.cs
routes.MapRoute(
name: "Default2",
url: "Admin/ViewCustomerDetails/{c_name}",
defaults: new { controller = "Admin", action = "ViewCustomerDetails", c_name= UrlParameter.Optional }
);

You didn't pass the parameter to the controller.
You can always simply pass parameters as part of query string as long as action method on controller is expecting them by exactly the the same name in the signature.

Related

Redirect To Action method not mapping correctly

I'm calling RedirectToAction but it isn't working properly.
I want the resulting URL to look like this:
https://localhost:44301/ManageSpaces/123/overview
but it looks like this and is missing the action portion of the URL:
https://localhost:44301/ManageSpaces/123
Here is my RedirectToAction call.
return RedirectToAction("overview", new RouteValueDictionary(
new {controller = "ManageSpaces", action = "overview", id = 123}));
Here is what my route looks like in RouteConfig:
routes.MapRoute("ManageSpaces",
"ManageSpaces/{id}/{action}",
new { controller = "ManageSpaces", action = "overview"},
new { id = #"\d+" } //The regular expression \d+ matches one or more integers
);
Maybe it is taking the default route. Rename, remove, or comment out the default route to see if that has any effect.
You have made your action route value optional by providing a default value. Optional values are ignored when resolving the URL.
routes.MapRoute("ManageSpaces",
"ManageSpaces/{id}/{action}",
new { controller = "ManageSpaces", action = "overview"},
new { id = #"\d+" } //The regular expression \d+ matches one or more integers
);
If you want to include the action in the URL, you have to make it a required argument.
routes.MapRoute("ManageSpaces",
"ManageSpaces/{id}/{action}",
new { controller = "ManageSpaces"},
new { id = #"\d+" } //The regular expression \d+ matches one or more integers
);

Why use "new" for Route Values in "ActionLink" ---- for example

I have 2 examples below:
#Html.ActionLink("Create New", "Create", new { id = Model.Id })
and,
return RedirectToAction("Index", new { id = review.RestaurantId });
My question is regarding the new { id = xxx} part in object route values. Why do we use "new" in this case? What exactly does it do? Does it initialize "id" variable in this case?
Also, it is strange that these methods, create and index definition can only take arguments as defined in the route values...
That is,
public ActionResult create { int id)
{ ...}
is correct but following is wrong....
public ActionResult create { int somethingelse)
{ ...}
So please tell me what is the new {id = xx} in my first 2 examples is doing?
Thanks
new {} creates a new object of type Object. The type is anonymous. You see that syntax when writing linq queries that end in " select new {x = "foo". y="bar"}". It is often used when setting an object to type "var".
What you are doing in your ActionLink is providing Route Values. MVC takes the properties and values in the object and puts them in the QueryString of the request. It is what you might refer to as "magic". You can set a break point in your controller Action and check "HttpContext.Request.QueryString" to see it.
The input values for you Action methods have to match the properties that are being passed in via the QueryString.
That is actually creating an anonymously typed object and passing it into ActionLink(). ActionLink then uses that object, coupled with your routing rules to generate the link. MVC will look for properties on that object that match the routing names (usually of route parameters) and figure out how to build it. Since you likely have the default MVC route (/controller/action/{id}) that is what links everything together.
Further, that is why id "is correct", but somethingelse "is wrong".
If you change "id" to "somethingelse" in your routing rule, you could then see new { soemthingelse = ""} work in your ActionLink().
Does that help?
In both cases your creating a new anonymous object to pass into the query string as a route value. You create a new object because one does not already exist on the view.
The MVC source code:
if (additionalViewData != null) {
foreach (KeyValuePair<string, object> kvp in new RouteValueDictionary(additionalViewData)) {
viewData[kvp.Key] = kvp.Value;
}
}
They use it to create new RouteValueDictionary parameters.
You don't have to use it the this manor. You could create an object on the model and pass it in:
public class SomeModel
{
public SomeModel()
{
MyObject = new { id = 10 };
}
public int Id {get;set;}
public object MyObject {get;set;}
}
#Html.ActionLink("Create New", "Create", Model.MyObject)
This would also work though is probably not something you would attempt.
For the second part of your question. The RouteValueDictionary searches by key and assigns the value to the key that was given.
So whatever you call the key in the anonymous object, MVC will attempt to assign the value to it on the action. The name must match or they key cannot assign the value.

ASP.NET MVC Map String Url To A Route Value Object

I am creating a modular ASP.NET MVC application using areas. In short, I have created a greedy route that captures all routes beginning with {application}/{*catchAll}.
Here is the action:
// get /application/index
public ActionResult Index(string application, object catchAll)
{
// forward to partial request to return partial view
ViewData["partialRequest"] = new PartialRequest(catchAll);
// this gets called in the view page and uses a partial request class to return a partial view
}
Example:
The Url "/Application/Accounts/LogOn" will then cause the Index action to pass "/Accounts/LogOn" into the PartialRequest, but as a string value.
// partial request constructor
public PartialRequest(object routeValues)
{
RouteValueDictionary = new RouteValueDictionary(routeValues);
}
In this case, the route value dictionary will not return any values for the routeData, whereas if I specify a route in the Index Action:
ViewData["partialRequest"] = new PartialRequest(new { controller = "accounts", action = "logon" });
It works, and the routeData values contains a "controller" key and an "action" key; whereas before, the keys are empty, and therefore the rest of the class wont work.
So my question is, how can I convert the "/Accounts/LogOn" in the catchAll to "new { controller = "accounts", action = "logon" }"??
If this is not clear, I will explain more! :)
Matt
This is the "closest" I have got, but it obviously wont work for complex routes:
// split values into array
var routeParts = catchAll.ToString().Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
// feels like a hack
catchAll = new
{
controller = routeParts[0],
action = routeParts[1]
};
You need to know what part is what in the catchAll parameter. Then you need to parse it yourself (like you are doing in your example or use a regexp). There is no way for the framework to know what part is the controller name and what is the action name and so on, as you haven't specified that in your route.
Why do you want to do something like this? There is probably a better way.

ASP.Net MVC: Request variables through Routes and View method

Basically if I wanted to make something a search page with paging I would need a url like:
/Topics/Index?search=hi&page=1
What I can't seem to figure out is how to:
A) Set a default route with no search and page 1
/Topics/Index?page=1 or even /Topics/Index?search=&page=1
B) use the View method to do the same
I do see that if I have a method on the control:
Index(String search, Int32? page)
And use the url:
/Topics/Index?search=hi&page=1 or /Topics/Index?search=hi
It gives me what I want in the method. I just need a way to get a default route for the Topic controller to create a default url with said request variables. I just don't think that
/Topics/Index/hi/1
Is conducive to a search url, mostly because there's no guarantee I'll have search terms or a page so it could end up like:
/Topics/Index/1
Anything you pass in the RouteValueDictionary that doesn't map to a part of your Url will get added as a querystring parameter. So you can do:
Url.GenerateUrl("Route", "Index", "Topics",
new RouteValueDictionary(new
{
page = this.Model.PageNumber,
search = this.Model.Search
});
So basically I resorted to handling the non values by setting up defaults on the controller. Not sure this is the best idea though.
In GLobal.asax:
routes.MapRoute
(
"TopicDefault",
"Topic/{action}",
new { controller = "Topic", action = "Index"}
);
On the Controller:
public ActionResult Index(Int32? parentForumId, Int32? pageNumber, Int32? amountToShow)
{
Int32 revisedPageNumber = pageNumber.HasValue ? pageNumber.Value : 0;
Int32 revisedAmountToShow = amountToShow.HasValue ? amountToShow.Value : 10;
Int32 revisedParentForumId = parentForumId.HasValue ? parentForumId.Value : 1;
IList<TopicCreateViewModel> modelValues =
Topic.GetListForGrid(revisedParentForumId, revisedPageNumber,
revisedAmountToShow, out realPage)
return View("Index", modelValues);
}

ASP.NET MVC - Mapping more than one query string parameter to a pretty url

I am a bit stuck on the design of my seo friendly urls for mvc....Take for example the following url:
http://myapp/venues/resturants.aspx?location=central&orderBy=top-rated
With my mvc app i have mapped it as follows:
http://myapp/venues/list/resturants/central/top-rated
{controller}/{action}/{category}/{location}/{order}
Now the only problem is that location and order are optional...so it should be possible to submit a request like: http://myapp/venues/list/resturants/top-rated . This proves to be a problem when the request hits the controller action, the location parameter has picked up "top-rated", naturally.
Any suggestions? I' am considering using explicit querystrings to handle more than one parameter but this is really my last option as i dont want to sacrifice SEO too much.
Has anyone eles run into such dilemmas? And how did you handle it?
Thanks in advance!
Click on your profile link and look at the URLs for Stats, Recent, Response, etc.
Examples:
https://stackoverflow.com/users/52065?sort=recent#sort-top
https://stackoverflow.com/users/52065?sort=stats#sort-top
with no sort it defaults to stats
https://stackoverflow.com/users/52065
Optional paramters should be query parameters
Assuming that the allowed values for location and order are unique (i.e. when they come in, you can tell them apart, or else if they only supply one, how are you going to know if it's a location or an order?), then you could just take two parameters and work out what they are in the controller.
Route: {controller}/{action}/{param1}/{param2}
Controller action:
public ActionResult MyAction(string param1, string param2)
{
string location;
string order;
if (!ParseLocation(param1, out location))
{ ParseLocation(param2, out location); }
// ...
}
Not particularly elegant, but does let you have the URLs you want.
You will always have this issue if you have multiple optional parameters. Either make one or both of them non-optional (and positioned earlier in the query string than the optional one) or use the querystring parameter notation.
ok guys just posting a solution i've been playing with so far.
I have set up my routes using constraints as follows:
routes.MapRoute(
"VenuesList",
"venues/list/{category}/{location}/{orderBy}",
new { controller = "venues", action = "list", category = "", location = "", orderBy = "" },
new { location = "central|east|west|south", orderBy = "top-rated|price" }
);
routes.MapRoute(
"VenuesListByLocation",
"venues/list/{category}/{location}",
new { controller = "venues", action = "list", category = "", location = "" },
new { location = "central|east|west|south" }
);
routes.MapRoute(
"VenuesListByOrder",
"venues/list/{category}/{orderBy}",
new { controller = "venues", action = "list", category = "", orderBy = "" },
new { orderBy = "top-rated|price" }
);
routes.MapRoute(
"VenuesListDefault",
"venues/list/{category}",
new { controller = "venues", action = "list", category = "" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
The idea is that if the validation fails it will go to the next route in the list...eventually hitting the default.
Needs some more testing but has worked well so far...
Why don't you create a property in the page for each possible querystring parameter?
This way you can handle it any way you choose with just a few lines of code...

Resources