【问题标题】:Input builder for a DropDownList with data from DB in mvc contribDropDownList 的输入构建器,其中包含来自 mvc contrib 中的 DB 的数据
【发布时间】:2009-11-12 12:42:27
【问题描述】:

我有这样的事情

public class Person
{
public Country {get; set;}

}

public class PersonInput
{
public ImNotSureWhatShouldIUseHere Country {get; set;}
}

在 mvc contrib 中有一个用于枚举的输入构建器,但这对我不利,因为我从数据库中检索数据并且我保存了所选元素的 Id 而不是值

【问题讨论】:

    标签: asp.net-mvc mvccontrib input-builders


    【解决方案1】:

    我自己做了一个输入构建器,看起来像这样

    <%@ 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); } }
    

    【讨论】:

      【解决方案2】:

      让我试着回顾一下我从您的问题中了解到的内容:您有一个数据库表 countries,其中包含您想绑定到视图中的 select 元素的列 idname。如果这是正确的,您可以尝试这样做:

      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
          ...
      }
      

      【讨论】:

      • 实际上我正在尝试使用 MVC Contrib 输入构建器,我知道 mvc 的内置内容 :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-18
      • 1970-01-01
      • 1970-01-01
      • 2021-12-05
      • 1970-01-01
      • 1970-01-01
      • 2019-12-16
      相关资源
      最近更新 更多