来自不同型号的Asp MVC 5下拉列表

时间:2014-09-16 18:56:07

标签: asp.net html.dropdownlistfor

我正在使用实体框架6.1.0在mvc 5的asp.net中构建一个项目。我的模型包含数字格式的一些值,1个模型包含该值的数值,域和描述(含义)。

如何创建包含模型中该值描述的下拉列表。

我将在sql查询中向您展示,如果需要,我可以发布任何代码。

select
value, 
description 
from
table t1
inner join table2 t2 on t2.number_value = t1.value
where
t1.domain = 'COLORS';

此查询例如将为我提供COLORS域的所有描述。像这样:

Value  Description  Domain
1      Yellow       COLORS
2      Blue         COLORS
3      Red          COLORS

假设我想要记录1辆这样的车:

name  model  year  color
Opel  Astra  2014   2

在我的模型编辑视图中,我想显示包含所有COLORS值的下拉列表,但我只想存储选定的值。

稍后我想再次显示下拉列表,但选择了选项。

你能帮我吗?

型号:

    public partial class korisnik_detalji
    {
        public int id { get; set; }
        public int korisnik_id { get; set; }
        public string ime { get; set; }
        public string prezime { get; set; }
        public string adresa { get; set; }
        public string grad { get; set; }
        public string drzava { get; set; }
        public Nullable<int> mj_tezine { get; set; }
        public Nullable<int> mj_tekucine { get; set; }
        public string slika { get; set; }

        public virtual korisnik korisnik { get; set; }
    }
//This is model with values and descriptions

    public partial class mapiranja
    {
        public int vrijednost { get; set; }
        public string opis { get; set; }
        public string domena { get; set; }
        public int id { get; set; }
    }

查看:

@model eVinogradarstvo.Models.korisnik_detalji
@using Microsoft.AspNet.Identity
@{
    ViewBag.Title = "Detalji";
}
<div id="main_middle">
    <div id="detalji">
@if (Request.IsAuthenticated)
{
        <h2>@Html.DisplayFor(model => model.korisnik.UserName)</h2>
        <div class="profil">
            <div class="slika">
            @if (Model.slika != null)
            { 
                <img src="@Url.Content(Model.slika)" />
            }
            </div>
            <div>
                <ul>
                    <li>
                        @Html.ActionLink("Promijeni sliku", null)
                    </li>
                    <li>
                        @Html.ActionLink("Promijeni lozinku", "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" })
                    </li>
                    <li>
                        @Html.ActionLink("Promijeni podatke", "Edit", new { id = Model.id })
                    </li>
                </ul>
            </div>
        </div>
        <div class="podaci">
            <ul>
                <li>
                    <label>Ime</label>
                  @if (Model.ime != null)
                  {
                    @Html.DisplayFor(model => model.ime)
                  }
                </li>
                <li>
                    <label>Prezime</label>
                @if (Model.prezime != null)
                { 
                    @Html.DisplayFor(model => model.prezime)
                }

                </li>
                <li>
                    <label>Adresa</label>
                 @if (Model.adresa != null)
                 { 
                    @Html.DisplayFor(model => model.adresa)
                 }
                </li>
                <li>
                    <label>Grad</label>
                @if (Model.grad != null)
                { 
                    @Html.DisplayFor(model => model.grad)
                }
                </li>
                <li>
                    <label>Država</label>
                @if (Model.drzava != null)
                {
                    @Html.DisplayFor(model => model.drzava)
                }
                </li>
                <li>
                    <label>Mjerna jedinica težine</label>
                @if (Model.mj_tezine != null)
                {
                    @Html.DisplayFor(model => model.mj_tezine)
                }
                </li>
                <li>
                    <label>Mjerna jedinica tekućine</label>
                @if (Model.mj_tekucine != null)
                {
                    @Html.DisplayFor(model => model.mj_tekucine)
                }
                </li>
            </ul>
        </div>
}else
{
        <p style="font-size:16px">Molimo da se @Html.ActionLink("prijavite", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" }) ili @Html.ActionLink("registrirate", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</p>
}
    </div>
</div>

目前我正在显示mj_tezine和mj_tekucine的数字值,但我想获得这些值的描述。描述在模型mapiranja.opis中。数值在mapiranja.vrijednost

1 个答案:

答案 0 :(得分:0)

首先,创建一个ViewModel

查看模型

public class DetaljiViewModel {
    public korisnik_detalji kd { get; set; }
    public mapiranja m { get; set; }
    public IEnumerable<SelectListItem> DropDownItems { get; set; }
}

第二次更改您的视图以使用ViewModel(我将给出一个非常简短的视图来展示如何)

@model eVinogradarstvo.Models.DetaljiViewModel 
<h2>@Html.DisplayFor(model => Model.kd.prezime)</h2>
<h2>@Html.DisplayFor(model => Model.m.opis )</h2>
@Html.DropDownListFor(model => Model.m.[PropertyToSaveTheSelectedValue], Model.DropDownItems )

请将[PropertyToSaveTheSelectedValue]更改为您实际希望将所选值存储在

中的任何属性