0

Please look at my code. I am trying to display some dynamic customers in the dropdown menu. And after selecting a customer button is pressed. Then products owned by the selected customer should be displayed in the textarea. But the problem is that after selecting a customer and pressing the button, nothing is displayed in the text area !. As i am new to ASP MVC can you help me out this problem?

Controller class-------------------->

public class ProductsController : Controller
{

    CandidateEntities db;

    public ProductsController()
    {
        db = new CandidateEntities();
    }

    public ActionResult Index()
    {
        ViewData["Customers"] = new SelectList(db.Customer.ToList(), "CustomerId", "Firstname");
        return View();
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(FormCollection form)
    {
        int customerId = int.Parse(form["Customers"]);
        var cust = db.Customer.First(x=>x.CustomerId == customerId);
        var Query =
            from customer in db.Customer
            join prod in db.Products on customerId equals prod.Customer.CustomerId
            select prod;
        ViewData["Products"] = Query.ToString(); 
        return View("Index");
    }



}

Index view------------------>

Index

<%using(Html.BeginForm()){ %>   
    <%=Html.DropDownList("Customers", "Seletc one")%>
    <input type="submit" value="Click !" />

    <%=Html.TextArea("Textarea", ViewData["Products"]) %>
<%} %>

2 Answers 2

2

Are you sure your query is returning results? It seems to be querying for every product for every customer, it might be rewritten something like:

from product in db.Prodcuts
where product.Customer.CustomerID = customerId
select product

Also, (and it depends on your code), but is calling ToString on a list of Product objects going to return what you want?

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

Comments

1
db.Products.Where(p => p.CustomerId == cust)

or

db.Customer.Find(cust).Products

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.