views:

343

answers:

2

I have something like this

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

}

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

there is a input builder for Enums in mvc contrib but it's not good for me because i retrieve the data from the DB and i save the Id of the selected element not the value

+1  A: 

Let me try to recap what I understood from your question: you have a database table countries that contains columns id and name that you would like to bind to a select element in a view. If this is correct you could try doing something like this:

public ActionResult Index()
{
    IEnumerable<Country> countries = FetchCountriesFromDB();
    var model = new SelectList(countries, "Id", "Name", null);
    return View(model);
}

And in your strongly typed view:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.SelectList>" %>
...
<%= Html.DropDownList("country", Model) %>

and finally you could have an action that you post to:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(string country)
{
    // country will contain the selected country id
    ...
}
Darin Dimitrov
actually i'm trying to use MVC Contrib input builders, i know about the mvc's built-in stuff :)
Omu
i managed to do a input builder of my own, so it's ok now
Omu
+1  A: 

i managed to do a input builder of my own, it looks like this

<%@ 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>

and the property in the Input class it's like this:

 [PartialView("MySelectList")]
        public IEnumerable<SelectListItem> Om { get { return new SelectList(Web.Models.DataGenerator.Persons, "Id", "Name", 2); } }
Omu