如何在下拉框中添加其他参数?

时间:2015-06-09 11:29:19

标签: c# html parameters

目前我有一个下拉框,它只显示一个供应商名称,其背后隐藏着ID值。我还想在供应商名称旁边显示供应商帐号。

HTML:

@Html.DropDownListFor(
    m => m.SupplierID,
    new SelectList(Model.Suppliers, "SupplierID", "SupplierName"),
    new { @id = "SuppNameDD", @class = "GRDropDown" }
)

控制器:

public ActionResult Index(string client) {
    int clientID = clientRepo.GetClientIDByName(client);
    DashboardViewModel model = new DashboardViewModel();
    model.ClientID = clientID;
    model.ClientName = client;
    model.FinancialsAtAGlance = reportRepo.GetFinancialsAtAGlance(model.ClientID);

    model.SupplierID = -1;
    model.AccountNo = null;
    model.Suppliers = supplierRepo.GetAllSuppliersByClient(clientID);

    model.ReviewID = -1;
    model.AccountNo = null;
    model.Reviews = reviewRepo.GetAllReviewsByClientID(clientID);
    return View(model);
}

视图模型:

public class DashboardViewModel {
    public int ClientID { get; set; }
    public string ClientName { get; set; }
    public IQueryable<FinancialsAtAGlanceModel> FinancialsAtAGlance { get; set; }
    public Dictionary<string, Dictionary<string, decimal?>> Budgets { get; set; }


    public class SelectReport {
        public int ReportID { get; set; }
        public string ReportType { get; set; }

        public static IEnumerable<SelectReport> Reports = new List<SelectReport> {
            new SelectReport {
                ReportID = 1,
                ReportType = "Claims By Supplier"
            },
            new SelectReport {
                ReportID = 2,
                ReportType = "Department breakdown"
            },
            new SelectReport {
                ReportID = 3,
                ReportType = "Reason Code breakdown"
            },
            new SelectReport {
                ReportID = 4,
                ReportType = "Monthly Debiting report"
            }
        };
    }
    public List<SelectReport> allReports { get; set; }

    public int SupplierID { get; set; }
    public IEnumerable<Supplier> Suppliers { get; set; }

    public int ReviewID { get; set; }
    public string AccountNo { get; set; }
    public IEnumerable<Review> Reviews { get; set; }
}

如何添加这个是因为另一个值是选定的值,这不是我想要的。它应该是另一个datatext字段。

2 个答案:

答案 0 :(得分:0)

如果此显示名称是多次使用的内容,我建议您在Supplier课程中添加一个属性。像DisplayName

这样的东西
public class Supplier
{
    //...
    public string SupplierName { get; set; }
    public string AccountNumber { get; set; }
    //...

    public string DisplayName
    {
        get { return String.Format("{0} ({1})", SupplierName, AccountNumber); }
    }
}

然后,您只需更改下拉列表,使用DisplayName代替SupplierName作为文本字段:

@Html.DropDownListFor(m => m.SupplierID, new SelectList(Model.Suppliers, "SupplierID", "DisplayName"), new { @id = "SuppNameDD", @class = "GRDropDown" })

修改 还有另一种方法可以在视图中完成所有操作:

@Html.DropDownListFor(m => m.SupplierID, Model.Suppliers.Select(item => new SelectListItem
{
    Value = item.SupplierID.ToString(),
    Text = String.Format("{0} ({1})", item.SupplierName, item.AccountNumber.ToString()),
    Selected = item.SupplierID == Model.SupplierID
}))

答案 1 :(得分:0)

您可以通过以下方式实现所需的输出:使用扩展方法创建自定义帮助程序,该方法将返回MvcHtmlString,它将为下拉列表创建自定义HTML并在视图中调用该方法。

喜欢下面

public static class CustomDropdown
{
    public static string Dropdown(Priority priority)
    {       
        StringBuilder sb=new StringBuilder ();
        sb+="<Select id='drop'>";
        for(int i=0;i<priority.name.length;i++)
        {
            sb+="<option id='dropop' value='"+priority.value[i]+"'title='"+priority.title[i]+"'>"+priority.name[i]+"</option>";
        }
        sb+="</select>";
        return Convert.ToString(sb);                   
    }
}

2.在jquery的帮助下绑定给定选择的选项,如

var i=0;
$('.drpclass option').each(function(){
    $(this).attr('title',Model.priority.title[i])
    i++;
});