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

标签 c# asp.net-mvc asp.net-mvc-5

型号:

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 中。然后在表单回发时拆分。

例如-

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

关于c# - 如何从 MVC 中的 DropDownList 中检索所选项目的文本和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27803901/

相关文章:

c# - 网络服务接口(interface)?

jquery - 在MVC中打开模态弹出窗口时如何从列表中获取模态中的Id

asp.net-mvc - jQuery + Ajax + 加载旋转图像 = 太快了!

asp.net - Ninject owin 依赖项到 IIS Web 主机中以实现 ASP.net Identity

c# - 远程 Webdriver Chrome 抛出 "path to the driver executable"错误

c# - 将 arraylist 值插入数据库

asp.net-mvc - .NET Core 带密码的分布式 Redis 缓存

javascript - 在 MVC 中的同一 View 中填充第二个模型 : Prevent postback

jquery - 当我发布 MVC 5 网站时,相对路径不起作用?

c# - 使用 NPOI 将超链接从一个单元格复制到另一个单元格