Y
Y
Yahya2014-12-14 19:25:05
ASP.NET
Yahya, 2014-12-14 19:25:05

How to link two @html.dropdownlistfor?

Good evening!
When building a project in asp.net mvc, I got to the point where I need to link two dropdownlists, I build them this way:
public class HomeController : Controller
{
Database1dbContext _db = new Database1dbContext();
public ActionResult Index()
{
var p = from m in _db.Teams select m;
ViewBag.ddl = new SelectList(p, "Id", "Name");
var k = from h in _db.Players where h.TeamId == 1 select h;
ViewBag.ddl2 = new SelectList(k, "Id", "Name");
returnView();
}
}
Please tell me how can I specify the value from the table instead of 1 (where h.TeamId == 1) in this condition???

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Valery Abakumov, 2014-12-18
@Valeriy1991

Good afternoon!
First, I don't understand the question.

Please tell me how can I specify the value from the table instead of 1 (where h.TeamId == 1) in this condition???

And secondly, judging by the title
How to link two @html.dropdownlistfor
(if I understood it correctly), then on the server side (ie in C#) you will not link multiple dropdown lists. If you need that when you select any element from list #1 (let it be
) the elements in list #2 will automatically change (let it be
) , then you need to do the following:
1. using jQuery, hang on the change event of the select element [id="list_1"] handler
2. this handler will determine what value the user has selected in list #1 and send an ajax request to some url
3. on the server side, implement an action method that will receive a request parameter (from an ajax request) corresponding to the element selected in list No. 1, break into the database, receive filtered data and send it, for example, to View (which has @html .dropdownlistfor)
4. then on the ajax request (from step 2) on the success event or the done method , hang a handler that will parse the received html code and completely replace the contents of list No. 2 with a new html code.
But, again, this answer is based on a guess from the title of this post, because the question itself is ill-posed.
Good luck!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question