如何从 MVC 中的 DropDownList 中检索所选项目的文本和值

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

型号:

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; }
}

查看:

@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>
 }

在理想情况下,我希望获得用户在下拉列表中选择的整个 SelectListItem,而不仅仅是 model.SelectedSiteKey 中的值。在最坏的情况下,我想获得与隐藏字段中选择的值关联的文本 (model.SelectedSiteName)。

我的研究表明我可以使用 javascript,或者我可以使用 EditorTemplate,但我都不清楚该怎么做。

为了捕获一个额外的文本字符串,这似乎是很多工作。我是否缺少从选择的下拉列表中获取文本值的明显方法?

I just want to save the text value for display with my results without having to do another call back to the first db.

根据您的评论,您可以将 Text 和 Value 存储合并到 Value 中。然后在回传Form的时候拆分。

例如-

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();
}

创建两个字段,1 个值,2 个文本,

在 html 页面中 获取所选项目的文本并将其分配给文本字段

 <input id="text_field" name="text_field" type="hidden" />


@Html.DropDownList("value_field", null, "   ---Select ---", new { @id = "value", @class = "form-control selectpicker", required = "required", style = "width:180px" })
//jquery
  $('#value').change(function () {
        var text = $("option:selected", this).text();
        $('#text_field').val(text);
    });

这样你可以同时保存文本和值