Click here to Skip to main content
15,915,869 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

so , I have a method in my controller like so:

C#
[HttpPost]
public ActionResult Mycontroller(string RefNumber)
{
     CustomerEntities custObj = new CustomerEntities();
  var customer = from m in custObj.CustomerTbl select m;
   if (!string.IsNullOrEmpty(RefNumber))

{
   customer = CustomerTbl.Where(x => x.customerName.contains(RefNumber));
}
     return View("_Customer", customer);

}



This is where i am stuck, as i'm just learning MVC... The view ("_customer") is a partial view that contains a textfield and button to search for a customer using the reference number which is past as parameter in my controller method.

My question is how would get/pass the customer object to the partial and filter by reference number? e.g a customer's FirstName is to be displayed when query matches the reference number.

Thank you for your time.
Posted

1 solution

Your code doesn't make any sense at all. Oh, it also leaks resources.

1) You're creating a new DbContext (CustomerEntities), never using it and never disposing of it. This is your leak.

2) You're creating a IQueryable, called "customer", never using it then throwing it out.

3) The query you do run is on this "CustomerTbl", whatever that it is, where you're searching for a RefNumber inside of a customer name. Why would a "RefNumber" show up in a customer name? That doesn't make any sense.

4) You're not returning a single customer. You're returning a collection of customers. In your case, you may be returning a collection of Customers that only has one Customer in it.

5) You never create a view model object to pass to your view. You're passing a collection of database objects to your view. This is bad practice. The View Model class you create should contain only the data that your page is going to show/edit and the supporting data to accomplish that edit.

The controller is generally responsible for managing the view the client sees and uses. Arguably, it can be said that it is also responsible for translating between the view models and database models.

Generally, your controller code would look something like this:
C#
public ActionResult Mycontroller(string RefNumber)
{
    // Validate your inputs...

    // Automatically Dispose the EF context when we're done with it.
    using (var context = new CustomerEntities())
    {
        // IQueryable for customers.
        var customers = (from c in context.CustomerTbl
                         where c.customerName.Contains(RefNumber)
                         select new CustomerViewModel()
                         {
                             CustomerId = c.CustomerId,
                             CustomerName = c.customerName
                         }).ToList(); // Executes the query.
    }

    // You now have an IEnumerable<customerviewmodel> to return to the view.
     return View("_Customer", customers);
}</customerviewmodel>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900