Html.Dropdownlistfor() with Custom Parameter

how to custom Html.DropDownListFor suppet set name attributes in ASP.NET Core MVC

You can use another version of dropdownlist creator as

@Html.DropDownList(string "name", IEnumerable<SelectListItem> selectList, 
object htmlAttributes)

In your case:

@Html.DropDownList("myName", Html.GetEnumSelectList<TgNetJZ.CustomerBiaoshi>(),
new { @class = "form-control custom-select", @id = "ddd" }).

But your model binding to Customer.CustomerBiaoshi will not work, because model binding uses name attribute and you changed the name attribute to myName.

HTML.DropDownListFor not passing parameter to controller?

Change departamentoSelecionado to departmentoSelecionado . You spelt an extra letter a in departmentoSelecionado .

//public IActionResult Index( string searchString, string departamentoSelecionado = "", int page = 1)
public IActionResult Index( string searchString, string departmentoSelecionado = "", int page = 1)

The parameter in action method should be same as f => f.DepartmentoSelecionado.


Update 16/09/2020

Codes of controller

public class RH_CentroCustoController : Controller
{

//i want to receive the value in departamentoSelecionado !!
public IActionResult Index(string searchString, string departmentoSelecionado ="", int page = 1)
{
List<SelectListItem> selectList = new List<SelectListItem>();

IList<RH_Departamento> RH_Departamento = new List<RH_Departamento>(){
new RH_Departamento(){ Departamento="dep1" },
new RH_Departamento(){ Departamento="dep2" },
new RH_Departamento(){ Departamento="dep3" },
};//just for test
//foreach (var x in _context.RH_Departamento.ToList())
foreach (var x in RH_Departamento.ToList())
{
selectList.Add(new SelectListItem() { Text = x.Departamento, Value = x.Departamento });
}

//and then i add to the viewModel that i create the list
ViewModelCentroCusto vmcc = new ViewModelCentroCusto()
{
Departamentos = selectList
};

return View(vmcc);
}
}

Codes of View

@model xxx.Models.ViewModelCentroCusto
@{
Layout = null;
}

@using (Html.BeginForm("Index", "RH_CentroCusto", FormMethod.Get))
{
@Html.DropDownListFor(f => f.DepartmentoSelecionado, new SelectList(Model.Departamentos, "Value", "Text"), "Select");
<input type="submit" value="Save" />
}

Codes of Model

public class ViewModelCentroCusto
{
public IEnumerable<SelectListItem> Departamentos { get; set; }
public string DepartmentoSelecionado { get; set; }
}

public class RH_Departamento
{
public string Departamento { get; set; }
}

Test

Sample Image

how to use @Html.DropDownListFor with custom datatype

In your attempt of doing this, you have made some basic mistakes. First try to understand what's going behind the scene in MVC and what those keywords mean.

you have defined

@model IEnumerable<MvcApplication1.customer>

then you have the following code

@Html.DropDownListFor(m=>m.name, new SelectList(Model,"id","name"));

here, your m denotes the @model which you have initialised as an IEnumerable<MvcApplication1.customer>. You are trying to access a property called name in IEnumerable (m => m.name) and IEnumerable doesn't have such property. if you want to access your model like that for some reason, your @model should be a Customer object. Then you can access it like @Html.DropDownListFor(m=>m.name, new SelectList(Model,"id","name"));

in a comment, you've said that this code @Html.DropDownList("name", new SelectList(Model, "id", "name")); works fine. Yes, it works because you are creating a new html select element with the Id "name" and it list anything that is in the Model as select options.

Okay, enough explanation.

This is the solution that I suggest for your problem.

In you controller action, implement the code flow as follows.

var dropdownDataList = //select your data from the database or any place as a list;

var dropdownOptions = dropdownDataList.Select(d => new {
id = d.valueforid,
name = d.valueforname
});

ViewBag.DropdownListOptions = new SelectList(dropdownOptions, "id", "name");

return View();

now in your view, do the following.

@{
Layout = null;
}

@model MvcApplication1.customer

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
@Html.DropDownListFor(m=>m.name, (SelectList)ViewBag.DropdownListOptions)
</div>
</body>
</html>

try to go through these articles in MSDN.
http://msdn.microsoft.com/en-us/library/gg416514(v=vs.108).aspx

hope this helps.

Cheers,
Amila



Related Topics



Leave a reply



Submit