Вы можете использовать OfType, чтобы сделать его более чистым:
IStepBuildDataSet buildDataSet = Steps.OfType<IStepBuildDataSet>().Single();
IStepBuildFile buildFile = Steps.OfType<IStepBuildFile>().Single();
Обратите внимание, вам не нужно отображать результаты, так как OfType делает это для вас.
Вот как вы это делаете:
public class TheModel
{
//Can be string, int, Guid, etc. Usually this is the index key.
public int SelectedValue {get;set;}
public List<SelectListItem> TheList {get;set;}
public TheModel()
{
TheList = new List<SelectListItem>();
}
}
Например, у вас есть модель с индексным ключом типа int
. Вы получаете его из базы данных и вставляете в список.
TheModel model = new TheModel();
using(MigrationDB db = new MigrationDB())
{
model.TheList = db.Plane.Select(m => new SelectListItem()
{
Value = m.PlaneId.ToString(), //Value can hold only string
Text = m.PlaneName //this is the <option>text</option>
}.ToList();
}
Вот как вы пишете это в виде Razor:
@model TheModel
.....
<form> //You can use Html.BeginForm() to create the form tag.
@Html.DropDownListFor(m => m.SelectedValue, //This will bind the selected option into `SelectedValue` property
Model.TheList, //This will fill up your <option> with the value from the list.
" Select Plane ", //If no value is selected this option will be selected with the value null
new { @class = "custom-class", ...} //Html attribute you want to put here.
);
</form>
Так что если вы выберете опцию из выпадающего списка, она будет привязана к свойству SelectedValue
.
[HttpPost]
public ActionResult PassTheValue(TheModel model) //Pass the entire model
{
var theSelectedValue = model.SelectedValue;
...
}
Вы должны передать всю модель из представления HTML-страницы в контроллер.
Ссылка на Html.BeginForm()
источник
Вы можете использовать Model
для формы. Это позволяет вам устанавливать значения по умолчанию в контроллере и обрабатывать их из формы POST.
Ваша модель просмотра
class PlaneFormModel
{
public string slctplane { get; set; }
public string Description { get; set; }
public string SaveInstance { get; set; }
public string AddInstance { get; set; }
public string ActivePlane { get; set; }
public string RestartInstance { get; set; }
public string GeneratePassword { get; set; }
public string ResetDB { get; set; }
public string DelInstance { get; set; }
public string DropDownValueSelected { get; set; }
public string List<SelectListItem> DropDownValues { get; set; }
}
Ваш метод контроллера HttpGet
[HttpGet]
public ActionResult ClickDetails()
{
// set view model with dropdown values, also you can use linq from your list of values
// also you can set default value for dropdown
var formModel = new PlaneFormModel
{
DropDownValueSelected = "defaultValue", // <---- value
DropDownValues = new List<SelectListItem>
{
new SelectListItem
{
Key = "key",
Value = "val"
}
}
};
return View(formMmodel);
}
Ваш метод контроллера HttpPost
[HttpPost]
public ActionResult ClickDetails(PlaneFormModel formModel)
{
// bind model from form also you can use FormCollection instead.
if (!string.IsNullOrEmpty(formModel.DropDownValueSelected))
{
...
}
...
}
И с этой точки зрения просто используйте шаблон помощника бритвы
@Html.DropDownListFor(m => m.DropDownValueSelected,
Model.DropDownValues,
"Select plane")