How to get selected item both text and value from DropDownList in MVC

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

      

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

      

In a perfect world, I would like to get the entire SelectListItem that the user has selected in the dropdown, not just the value in the model. SededSiteKey. In the worst case, I would like to get the text associated with the value selected in the hidden field (model.SelectedSiteName).

My research shows that I can use javascript, or I could use an EditorTemplate, which I don't know how to do.

It seems like a lot of work to catch one extra text line. Am I missing an obvious way to get a text value from a dropdown?

+3


source to share


1 answer


I just want to store the text value to display with my results without having to make another call back to the first db.

Based on your comment, you can concatenate both Text and value inside Value . Then split it when the form is submitted back.



For example -

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

      

+6


source







All Articles