Reputation: 533
I'm trying to get data from view in string
Here's the code in View
<a href="/Admin/ViewCustomerDetails/@customer.name"> </a>
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?
Upvotes: 0
Views: 37
Reputation: 35
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.
Upvotes: 0
Reputation: 536
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 }
);
Upvotes: 0
Reputation: 218732
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.
Upvotes: 2
Reputation: 1410
You should send it like this:
<a href="/Admin/[email protected]"> </a>
And make sure that @customer.name
is not null before going to the server side.
Upvotes: 0