输入建设者在MVC的contrib与数据一个DropDownList从数据库
-
19-09-2019 - |
题
我有这样的事
public class Person
{
public Country {get; set;}
}
public class PersonInput
{
public ImNotSureWhatShouldIUseHere Country {get; set;}
}
有在MVC的contrib枚举一个输入建设者而是因为我检索数据库中的数据是不适合我,我保存所选元素的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