MVCのcontribでDBからのデータとのDropDownListの入力ビルダー
-
19-09-2019 - |
質問
私はこのようなものを持っている。
public class Person
{
public Country {get; set;}
}
public class PersonInput
{
public ImNotSureWhatShouldIUseHere Country {get; set;}
}
が列挙型の入力ビルダーは、MVCのcontribにあるが、私はDBからデータを取得し、私が選択した要素ではない値
のIDを保存するので、それは私のために良いではありません解決
私は、私自身の入力ビルダーを行うために管理、それはこのようになります。
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/InputBuilders/Field.Master"
Inherits="System.Web.Mvc.ViewPage<PropertyViewModel<object>>" %>
<%@ Import Namespace="MvcContrib.UI.InputBuilder.Views"%>
<%@ Import Namespace="System.Web.Mvc.Html"%>
<asp:Content ID="Content2" ContentPlaceHolderID="Input" runat="server">
<%=Html.DropDownList(Model.Name, Model.Value as IEnumerable<SelectListItem>)%>
</asp:Content>
と入力クラスのプロパティ、それはこのようなものだ。
[PartialView("MySelectList")]
public IEnumerable<SelectListItem> Om { get { return new SelectList(Web.Models.DataGenerator.Persons, "Id", "Name", 2); } }
他のヒント
私はあなたの質問から分かることを復習してみましょう:あなたはビューのcountries
要素にバインドしたい列id
とname
を含むデータベーステーブルselect
を持っています。これが正しければ、あなたがこのような何かをやってみてください可能性があります:
public ActionResult Index()
{
IEnumerable<Country> countries = FetchCountriesFromDB();
var model = new SelectList(countries, "Id", "Name", null);
return View(model);
}
そして、あなたの強く型付けされたビューでます:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.SelectList>" %>
...
<%= Html.DropDownList("country", Model) %>
そして最後に、あなたがに投稿作用を有する可能性があります:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(string country)
{
// country will contain the selected country id
...
}
所属していません StackOverflow