How can I retrieve selected item's both Text and Value from DropDownList in MVC

Jen*_*r S 3 c# asp.net-mvc asp.net-mvc-5

Model:

public class SelectBillingSiteReportModel
{

    public IEnumerable<SelectListItem> Sites { get; set; }
    public string SelectedSiteName { get; set; }
    public string SelectedSiteKey { get; set; }
    [Required]
    [DataType(DataType.Date)]
    public DateTime FromDateTime { get; set; }
    [Required]
    [DataType(DataType.Date)]
    public DateTime ToDateTime { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

View:

@model MuseReport.Models.SelectBillingSiteReportModel
@{
    ViewBag.Title = "Select Site and Date Range";
}
    <div class="page-header">
    <h4>Select Site and Date Range for Ecg Billing Summary</h4>
</div>
<div class="row">
@using (Html.BeginForm("EcgBilling", "BillingRpt"))
{
    <div class ="form-horizontal" role="form">
        <div class="form-group">
         @Html.Label( "Muse Site", new { @class = "col-md-2 control-label"})
        <div class="col-md-10">@Html.DropDownListFor(model => model.SelectedSiteKey, Model.Sites, new { id="museSites", @class = "selectpicker"})</div>  
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.FromDateTime, "From Date", new { @class = "col-md-2 control-label"})
            <div class="col-md-10">@Html.EditorFor(model => model.FromDateTime)</div>  
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.ToDateTime, "To Date", new { @class = "col-md-2 control-label"})
            <div class="col-md-10">@Html.EditorFor(model => model.ToDateTime)</div>  
        </div>
        <div class="form-group">
            <div class="col-md-10">@Html.HiddenFor(model => model.SelectedSiteName)</div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <button type="submit" class="btn btn-primary">Go</button>
            </div>
        </div>
    </div>
 }
Run Code Online (Sandbox Code Playgroud)

在一个完美的世界中,我想获得用户在下拉列表中选择的整个SelectListItem,而不仅仅是model.SelectedSiteKey中的Value.在最坏的情况下,我希望将与选中的值相关联的Text与隐藏字段(model.SelectedSiteName)相关联.

我的研究表明我可以使用javascript,或者我可以使用EditorTemplate,我都不清楚如何做.

看起来这只是为了捕获一个额外的文本字符串.我是否错过了从选择下拉列表中获取文本值的明显方法?

Win*_*Win 6

我只想用我的结果保存显示的文本值,而不必再回到第一个数据库.

基于您的评论,你可以结合这两个文本和值内储存的价值.然后在回发表单时将其拆分.

例如 -

public ActionResult EcgBilling()
{
    var model = new SelectBillingSiteReportModel
    {
        Sites = new List<SelectListItem>
        {
            new SelectListItem {Text = "One", Value = "One:1"},
            new SelectListItem {Text = "Two", Value = "Two:2"},
            new SelectListItem {Text = "Three", Value = "Three:3"},
        }
    };
    return View(model);
}

[HttpPost]
public ActionResult EcgBilling(SelectBillingSiteReportModel model)
{
    string[] array = model.SelectedSiteKey.Split(':');
    string text = array[0];
    string value = array[1];
    return View();
}
Run Code Online (Sandbox Code Playgroud)