0

javascript\AJAXは使用されません。

4

3 に答える 3

2

以下のクラスは、リフレクションを使用して、リストで選択された値のテキストを取得します。複数選択されたリストアイテムはサポートされていません。

using System.Web.Mvc;

/// <summary>
/// Provides a set of static methods for getting the text for the selected value within the list.
/// </summary>
public static class SelectListExtensions
{
    /// <summary>
    /// Gets the text for the selected value.
    /// </summary>
    /// <param name="list">The list.</param>
    /// <returns></returns>
    public static string GetSelectedText(this SelectList list)
    {
        foreach(var item in list.Items)
        {
            var dataValuePropertyInfo = item.GetType().GetProperty(list.DataValueField);
            var itemValue = dataValuePropertyInfo.GetValue(item, null);

            if(itemValue != null && itemValue.Equals(list.SelectedValue))
            {
                var textValuePropertyInfo = item.GetType().GetProperty(list.DataTextField);
                return textValuePropertyInfo.GetValue(item, null) as string;
            }
        }

        return null;
    }
}
于 2010-04-20T12:09:42.157 に答える
0

あなたが試したことのいくつかのコードは、ここ黒桜で便利でしょう。

その間;

ビューをモデルにバインドしている場合は、UpdateModelを使用して値を取り戻すことができます。

したがって、Userというクラスにバインドすると、

User myUser = new User;
TryUpdateModel(myUser);

バインドしていない場合は、Eduardoの手法を使用して、次のようなものを使用します。

public ActionResult MyViewsAction(FormCollection collection)
{
  string a = collection["selectListCtrlname"];
}
于 2009-08-27T03:21:43.833 に答える
0

そうです、コードビハインドでリストを作成し、各オプションに一意の識別子を指定すると、その識別子を取得して、コード内のアイテム、つまりテキストと組み合わせることができます。

それで;

public class MonthlyItemsFormViewModel
{
  public SelectList Months;
  public string SelectedMonth {get;set;}
}

それから;

public ActionResult Index()
{
  MonthlyItemsFormViewModel fvm = new MonthlyItemsFormViewModel();
  FillData(fvm, DateTime.Now);
  return View(fvm);
}

その後;

private void FillData(MonthlyItemsFormViewModel fvm, DateTime SelectedMonth)
{
  List<string> months = DateTime.Now.MonthList(DateTime.Now);
  fvm.Months = new SelectList(months, fvm.SelectedMonth);
}

それからあなたの見解では;

<% using (Html.BeginForm()) { %>
  <%=Html.DropDownList("selectedMonth", Model.Months) %>
<%} %>

その後、ポストバックで;

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection collection)
{
  MonthlyItemsFormViewModel fvm = new MonthlyItemsFormViewModel();
  UpdateModel(fvm);
  FillData(fvm, DateTime.Parse(DateTime.Now.Year.ToString() + " " + fvm.SelectedMonth + " 01"));
  return View(fvm);
}

fvmから選択した値を取得し、その値を選択リストの項目と組み合わせることができるのは、ポストバックのコードにあります。

このコードは私のコードから直接削除されたため、状況に合わせて変更する必要があるかもしれません。

これは意味がありますか?

于 2009-08-27T03:59:40.233 に答える