0

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?

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Maybe customer.name is designed to be unique, like a username or something? Otherwise you are definitely right.
0

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.

Comments

0

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

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.