Flint Flint - 2 months ago 13
ASP.NET (C#) Question

ASP MVC Dropdown

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"]) %>
<%} %>

Answer

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?