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 :/