请查看我的代码。我想在下拉菜单中显示一些动态客户。然后按下选择客户按钮。然后,所选客户拥有的产品应显示在textarea中。但问题是在选择客户并按下按钮后,文本区域中没有显示任何内容!因为我是ASP MVC的新手你可以帮我解决这个问题吗?
控制器类-------------------->
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");
}
}
索引视图------------------>
<%using(Html.BeginForm()){ %>
<%=Html.DropDownList("Customers", "Seletc one")%>
<input type="submit" value="Click !" />
<%=Html.TextArea("Textarea", ViewData["Products"]) %>
<%} %>
答案 0 :(得分:2)
您确定您的查询是否返回结果?它似乎是为每个客户查询每件产品,它可能会被重写为:
from product in db.Prodcuts
where product.Customer.CustomerID = customerId
select product
此外,(并且它取决于您的代码),但是在Product对象列表上调用ToString将返回您想要的内容?
答案 1 :(得分:1)
db.Customer.Find(cust).Products
或
{{1}}