[RESOLVED]select a value in a combobox, search in the database and show the values that have this variable

Hi everyone can anyone help me  . . . I am totally new on MVC4 so but I have to create a website with this wonderful "world" 

I have a table "Func" with (id, name, idDepart) and a table "Depart" (idDepart, nome)

I need to have a combo with list of all Depart, select one X and list all of Func that have the Depart X, can anyone help me …

plz I really need that  . . . 

I have this , but doen´t work

// on my controller

public ActionResult consultaBD(int id)
{
string selected = (from Func in db.Depart
where Func.Id == id
select Func.NomeDepart).FirstOrDefault();
ViewBag.Departl_Id = new SelectList(db.Depart, "Id", "NomeDepart", selected);
Funcionar Func = db.Funcionar.Find(id);

if (Func == null)
{
return HttpNotFound();

}
return View( Func);
}

You can use the Combo box on select or on change event to fetch the list of functions related to the department.

Use the linq query to filter the records and send the list to the view.

var list = (from e in data where idDepart == idDepart select new SelectListItem { Text = e.Name, Value = e.Id }).ToList()

I think you cant return a LINQ result to a string, use ‘var’ instead..  On the client side( in the View) you have to use Ajax to fire the function for selected combo list value. 

Hi mgn_v  and mohammed

thanks for you replay but I didn´t understand what you mean Cry

can you explain  a bit more  . . . 

I have this on my cshtml:

<form action="/Produt/consultDB" method="post"><label>Select one </label> @Html.DropDownList("Depart_Id", string.Empty) <input type="submit" class="button success" value="Enter" /></form>

I´m following a exemple book  . . .  anyone with a good exemple or a way to resolve this . . . ?

just follow the following and do changes in view

public ActionResult consultaBD(int id)
{
string selectedDepartments = (from Func in db.Depart
where Func.Id == id
select new{Id=Func.DeptId,Name= Func.NomeDepart).ToList();
Funcionar Func = db.Funcionar.Find(id);
ViewBag.Departments=selectedDepartments;
if (Func == null)
{
return HttpNotFound();

}
return View();
}

<form action="/Produt/consultDB" method="post">
<label>Select one </label>
@Html.DropDownList("Department",new SelectList(ViewBag.Departments, "DeptId", "Name") )
<input type="submit" class="button success" value="Enter" />
</form>

Seshasayana thnks for your answer  . . .  but it doenst work, but it seems small error but I don´t know where Frown

I did this  . . .  to show all of "Perfil" in a combobox

 on my controller

public ActionResult ProdutoCategoria()

var model = db.Perfies;

ViewBag.Perfies_Id = new SelectList(db.Perfies.ToList(), "Id", "NomePerfil");

return View(model);
}

and this to select  . . .

public ActionResult consultaBD(int id)
{
var selectedPerfil = (from Perf in db.Perfies
where Perf.Id == id
select new { Id = Perf.Id, Name = Perf.NomePerfil }).ToList();
Utilizador utili = db.Utilizadores.Find(id);
ViewBag.Perfies_Id=selectedPerfil;

if (utili == null) {
return HttpNotFound();
}
return View();

and on view

<form action="/Produto/consultoDB" method="post">
<label>Select one </label>
@Html.DropDownList("Perfies_Id", new SelectList(ViewBag.Perfies_Id, "Id", "NomePerfil"))
<input type="submit" class="button success" value="Enter" />
</form>

is that allright   . . . ? 

Hi Figas,

First, the DropDownList’s name should match the parameter of action, so you need change parameter name and DropDownList’s name.

Secondly, the selected value is Depart ID, to get Func data, you need base on the foreign key
idDepart.

By the way, accoording to your description, your tables schema don’t match you code. (e.g. there isn’t the Id in Depart table)

There is a link that may benefit you:

# Cascading Dropdown List With MVC, LINQ to SQL and AJAX

http://www.codeproject.com/Articles/730953/Cascading-Dropdown-List-With-MVC-LINQ-to-SQL-and-A

Best Regards

Starain

Hi Figas,

Here i created a solution for cascadding dropdownlist .Smile

Under Product Controller:

public partial class ProductController : Controller
{

public virtual ActionResult ProdutoCategoria()

{
var products = new List<ProductsModel>
{
new ProductsModel
{
Id = 1,
Name = "Product1"
},
new ProductsModel
{
Id = 2,
Name = "Product2"
},
new ProductsModel
{
Id = 3,
Name = "Product3"
},
new ProductsModel
{
Id = 4,
Name = "Product4"
}
};
var model = new ProductModel
{
Products = products,
Departments = new List<DepartmentsModel>()
};
return View(model);
}

public virtual ActionResult ConsultaBD(int id)
{
var departments = new List<DepartmentsModel>
{
new DepartmentsModel
{
ProductId = 1,
Id = 1,
Name = "Department1"
},
new DepartmentsModel
{
ProductId = 1,
Id = 2,
Name = "Department2"
},
new DepartmentsModel
{
ProductId = 2,
Id = 3,
Name = "Department3"
},
new DepartmentsModel
{
ProductId = 2,
Id = 4,
Name = "Department4"
},
new DepartmentsModel
{
ProductId = 2,
Id = 5,
Name = "Department5"
},
new DepartmentsModel
{
ProductId = 3,
Id = 6,
Name = "Mobile6"
},
new DepartmentsModel
{
ProductId = 3,
Id = 7,
Name = "Department7"
},
new DepartmentsModel
{
ProductId = 4,
Id = 8,
Name = "Department8"
},
new DepartmentsModel
{
ProductId = 4,
Id = 9,
Name = "Department9"
}
};
var selectDepartment = (from d in departments where d.ProductId.Equals(id) select d).ToList();
return Json(selectDepartment, JsonRequestBehavior.AllowGet);
}

}

public class ProductsModel
{
public int Id { get; set; }

public string Name { get; set; }
}

public class DepartmentsModel
{

public int ProductId { get; set; }

public int Id { get; set; }

public string Name { get; set; }
}

public class ProductModel
{

public int ProductId { get; set; }
public int DepartmetId { get; set; }

public List<ProductsModel> Products { get; set; }

public List<DepartmentsModel> Departments { get; set; }
}

Under ProdutoCategoria View:

@model MVCTutorials.Controllers.ProductModel
@{
ViewBag.Title = "ProdutoCategoria";
}

<h2>ProdutoCategoria</h2>

@using (Html.BeginForm())
{
<label>Select Product </label>
@Html.DropDownListFor(m => m.ProductId, new SelectList(Model.Products, "Id", "Name"))
<label>Select Department </label>
@Html.DropDownListFor(m => m.DepartmetId, new SelectList(Model.Departments, "Id", "Name"))
<input type="submit" value="Enter" />

}

<script src="~/Scripts/jquery-1.8.3.js"></script>
<script type="text/javascript">

$(function () {

$(‘#ProductId’).change(function () {
var productId = $(this).val();
var departmentDdl = $(‘#DepartmetId’);
$.ajax({
type: "GET",
url: "/Product/ConsultaBD",//here Product is a controller namr and ConsultaBD is an action name
cache: false,
data: { "id": productId }, //here id is selected Product id
success: function (data) {
departmentDdl.html(”);
$.each(data, function (id, option) {
departmentDdl.append($(‘<option></option>’).val(option.Id).html(option.Name));
});
},
error: function () {
alert(‘Failed to retrieve Departments.’);
},
complete: function () {

}
});
});

});
</script>

Thanks

Seshasayana

Thanks guys your answers really help me
Seshasayana and Starain
Starain , by the way the article is awesome

Leave a Reply