0

Html.DropDownList を含む編集ページがあります....常に表示されるドロップダウンリストの値を表示することはできません。Select代わりに、モデル値に基づいて選択されたアイテムをドロップダウンに表示したいModel.Mes_Id...任意の提案どのようにそれを行うことができます...

       <p>
            <label for="MeasurementTypeId">MeasurementType:</label>
         <%= Html.DropDownList("MeasurementType", // what should i give here?)%>
            <%= Html.ValidationMessage("MeasurementTypeId", "*") %>
        </p>

編集:リスト項目がありますが、編集ビューで選択した値を表示したい...

   public ActionResult Edit(int id)
    {
        var mesurementTypes = consRepository.FindAllMeasurements();
        ViewData["MeasurementType"] = mesurementTypes;
        var material = consRepository.GetMaterial(id);
        return View("Edit", material);
    }

私のリポジトリメソッド、

public IEnumerable<SelectListItem> FindAllMeasurements()
        {
            var mesurements = from mt in db.MeasurementTypes
                              select new SelectListItem
                              {
                                 Value = mt.Id.ToString(),
                                 Text= mt.Name
                              };
            return mesurements;
        }
4

5 に答える 5

2

を作成するときに、選択したアイテムを設定しますIEnumerable<SelectListItem>

個人的には、フォーム専用のビューモデルを作成しますが、コードに従って、次のようにします。

public ActionResult Edit(int id)
{
    //Put this first
    var material = consRepository.GetMaterial(id);
    //pass in your selected item
    var mesurementTypes = consRepository.FindAllMeasurements(material.MeasurementTypeId);
    ViewData["MeasurementType"] = mesurementTypes;

    return View("Edit", material);
}

次に、リポジトリメソッドを次のように変更します。

public IEnumerable<SelectListItem> FindAllMeasurements(int selectedId)
{
     var mesurements = from mt in db.MeasurementTypes
                      select new SelectListItem
                      {
                         Value = mt.Id.ToString(),
                         Text= mt.Name,
                         Selected = mt.Id == selectedId
                      };

    return mesurements;
}

HTH、
チャールズ

于 2010-05-04T05:30:38.133 に答える
1

このブログエントリを見てください。

http://weblogs.asp.net/ashicmahtab/archive/2009/03/27/asp-net-mvc-html-dropdownlist-and-selected-value.aspx

mesurementTypes基本的に、リスト/列挙可能をSelectListまたはに変換する必要がありますIEnumerable<SelectListItem>

可能であれば、ASP.NET MVC2 にアップグレードして使用することをお勧めします。Html.DropDownListFor()

于 2010-05-04T05:14:03.167 に答える
0

選択した項目を指定できる SelectionList を返す必要があります。

ASP.NET MVC で DropDownList を作成する方法

于 2010-05-04T05:16:01.853 に答える
0

Html.DropDownListFor がうまくいかなかったので、このようなポピーを取得しました

    (in Edit method )

    CreatList(long.Parse(wc.ParentID.ToString()));


    private void CreatList(long selected= 0)
    {
        SqlConnection conn = new SqlConnection(Config.ConnectionStringSimple);
        conn.Open();
        Category wn = new Category(conn);
        CategoryCollection coll = new CategoryCollection();
        Category.FetchList(conn, ref coll);

        ViewBag.ParentID = GetList(coll, selected);   
    }


    private List<SelectListItem> GetList(CategoryCollection coll, long selected)
    {
        List<SelectListItem> st = new List<SelectListItem>();
        foreach (var cat in coll)
        {
            st.Add( new SelectListItem
            {
                Text = cat.Name,
                Value = cat.ID.ToString(),
                Selected = cat.ID == selected
            });

        }
        SelectListItem s = new SelectListItem { 
                                Text = Resources.lblSelect, 
                                Value = "0" 
        };
        st.Insert(0, s);
        return st;
    }


    <div class="editor-label">
        @Html.LabelFor(model => model.ParentID)
    </div>
    <div class="editor-field">
        @Html.DropDownList("ddlCat", (List<SelectListItem>)ViewBag.ParentID)
        @Html.ValidationMessageFor(model => model.ParentID)
    </div>
于 2013-03-06T19:07:18.193 に答える
0

Model.Mes_Id に選択した値が含まれていると仮定すると、次のようなことができます

<%
    var Measurements = new SelectList((IEnumerable)ViewData["MeasurementType"], "Id", "Name", Model.Mes_Id);
    Response.Write(Html.DropDownList("measurement_type", Measurements, "Select"));
%>
于 2010-05-04T05:26:52.587 に答える