كيفية جعل القائمة المنسدلة تظهر قيمة محددة في ASP.NET MVC؟

StackOverflow https://stackoverflow.com/questions/2762845

سؤال

لدي صفحة تحرير مع 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;
        }
هل كانت مفيدة؟

المحلول

اضبط العنصر المحدد عند إنشاء ملف 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;
}

HTHS ،
تشارلز

نصائح أخرى

إلقاء نظرة على إدخال المدونة هذا.

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

في الأساس ، تحتاج إلى تحويل الخاص بك mesurementTypes قائمة/تعداد في أ SelectList أو IEnumerable<SelectListItem>.

أوصي ، إن أمكن ، بالترقية إلى ASP.NET MVC2 واستخدام Html.DropDownListFor()

يجب أن تقوم بإرجاع قائمة تحديد يمكنها تحديد عنصر محدد.

كيفية إنشاء قائمة منسدلة مع ASP.NET MVC

على افتراض أن النموذج.

<%
    var Measurements = new SelectList((IEnumerable)ViewData["MeasurementType"], "Id", "Name", Model.Mes_Id);
    Response.Write(Html.DropDownList("measurement_type", Measurements, "Select"));
%>

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>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top