Html.DropDownList wysyłanie wybranego elementu akcją formularza

0

Od niedawna dłubie sobie w .net więc nie wiem jak bardzo newbie to jest problem. Mam kawałek kodu MVC, w widoku mam formularz z listą wyboru, wykonując posta chce przesłać wybrany element bindując go do propertiesa modelu. Trudność (albo i nie) polega na tym, że mam tak jakby pozagnieżdżane dwa modele i widoki.

Kontroler:

public class ThermographyController : System.Web.Mvc.Controller
    {
        public ViewResult Index()
        {
            return View("Index",new ThermographyResource());
        }

        [HttpPost]
        public ActionResult ThankYou(ThermographyResource resource)
        {
            // tutaj bedzie troche inny kod, zostawiłem tak żeby nie zaciemniać
            return View("Index", resource);
        }
    }
 

Główny model i widok:

    [Serializable]
    public class ThermographyResource
    {
        public ThermographyResource()
        {
            BuildingInfo = new BuildingInfo();
        }

        public BuildingInfo BuildingInfo { get; private set;}
    } 
@model VResource.ThermographyResource

@using (Html.BeginForm("ThankYou","Thermography",FormMethod.Post,Model))
{
            <div class="right">
                <input type="submit" class="css3-btn css3-btn-r" value="" />
            </div>
    @Html.Partial("_BuildingInfo", Model.BuildingInfo)
}

Zagnieżdżony model i widok:

 
public class BuildingInfo
    {
        public BuildingInfo()
        {
            Title = "Title";
            OwnerRenterDropDownValues = GetOwnerRenterDropDownValues();
            OwnerRenter = OwnerRenterDropDownValues.Single(or => or.Selected).Value;
        }


        public string Title { get; private set; }

        [DataType(DataType.Text)]
        [DisplayName("Owner/Renter")]
        public string OwnerRenter { get; set; }// <-- tutaj chciałbym przechowywać wartość wybranego elementu

        public IEnumerable<SelectListItem> OwnerRenterDropDownValues { get; private set; }

        private IEnumerable<SelectListItem> GetOwnerRenterDropDownValues() {...}
@model Resource.BuildingInfo

@Html.Label(Model.Title)

<div class="ui-grid-a" style="position: relative;left: -5px;">
    <div class="ui-block-a">
        @Html.LabelFor(m => m.OwnerRenter)
        @Html.DropDownList(Model.OwnerRenter, Model.OwnerRenterDropDownValues, new { @class = "input"})      
    </div>
</div>

No, i jak robię posta to w kontrolerze wartość wybranego posta nie binduje mi się do mojego propertiesa. Najgorsze jest to, że w innym miejscu ta "sztuczka" mi się udała w bardzo podobny sposób jak tutaj, ale patrzę na to tak długo że chyba już nie widzę gdzie jest różnica :/

0

Uzyj Html.DropDownListFor:

@html.DropDownListFor(x => x.OwnerRenter, Model.OwnerRenterDropDownValues, new { @class = "input"})

0

@up już spróbowałem w między czasie i nic nie dało, gdzieś muszę mieć jakiś subtelny błąd...

EDIT

Znalazłem przyczynę, ten partial jednak psuł sprawę. Zamiast partiala użyłem EditorFor i wszystko zadziałało. Tak więc mój widok główny wygląda teraz tak:

@model VResource.ThermographyResource
 
@using (Html.BeginForm("ThankYou","Thermography",FormMethod.Post,Model))
{
            <div class="right">
                <input type="submit" class="css3-btn css3-btn-r" value="" />
            </div>
    @Html.EditorFor(m => m.BuildingInfo)
} 

1 użytkowników online, w tym zalogowanych: 0, gości: 1