Good morning,
The idea is the following: I have a project in ASP.NET 4.6.2 following the MVVM pattern, I have my ViewModel class that represents the page and one of the properties of that ViewModel is an object that has another object as its property. Step to code what is written:
public class AlumnoViewModel : BaseViewModel
{
public List<AlumnoModel> Alumnos { get; set; }
public AlumnoModel SearchEntity { get; set; }
public AlumnoModel AddEntity { get; set; }
public int TurnoId { get; set; }
public int CarreraId { get; set; }
public IEnumerable<SelectListItem> Carreras { get; set; }
public IEnumerable<SelectListItem> Turnos { get; set; }
}
The AddEntity
property (which is a StudentModel) would be my LITTLE that will then be mapped with Automapper to your domain class for persistence.
I show how is my AddEntity:
public class AlumnoModel
{
[Display(Name ="Nombre")]
public string Nombre { get; set; }
[Display(Name = "Apellido")]
public string Apellido { get; set; }
[Display(Name = "N° Legajo")]
public string Legajo { get; set; }
[Display(Name = "Dni")]
public int Dni { get; set; }
[Display(Name = "Carrera")]
public CarreraModel Carrera { get; set; }
[Display(Name = "Turno")]
public TurnoModel Turno { get; set; }
}
Almost all the properties of this class are strongly typed except for two, which will be Carrera and Turn which are in turn two other POCOs. I show one because they are identical:
public class TurnoModel
{
public int TurnoId { get; set; }
[Display(Name = "Turno")]
public string Descripcion { get; set; }
}
Then the idea is the following: My AddEntity
represents a student, that student has several properties, also has a shift and a career, so you can see it is a ratio of 1 an between the table Turns, Race with Students.
Now, the idea is that when wanting to save a Student from the presentation layer to my database my AddEntity
passes to the controller with all its data LESS those that are complex objects. The user chooses a turn and a race through a @Html.DropDownListFor
step to show this part of the view:
<div class="form-group">
@Html.LabelFor(m => m.AddEntity.Turno)
@Html.DropDownListFor(m => m.Turnos, Model.Turnos, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.AddEntity.Carrera)
@Html.DropDownListFor(m => m.Carreras, Model.Carreras, new { @class = "form-control" })
</div>
As you can see, I'm creating two DropDown with the ViewModel's Races and Turns properties that are two SelectListItem.
What you try to do is, by jQuery, capture the selected value in these two DropDown and pass it to these two properties that pass through two HiddenFor
. These are my Hidden:
@Html.HiddenFor(m => m.AddEntity.Carrera.CarreraId)
@Html.HiddenFor(m => m.AddEntity.Turno.TurnoId)
And here the jQuery:
if (action === "Save")
{
var carreraId = parseInt($("#Carreras option:selected").val());
var turnoId = parseInt($("#Turnos option:selected").val());
$("#AddEntity_Turno_TurnoId").val(turnoId);
$("#AddEntity_Carrera_CarreraId").val(carreraId);
}
The question would be, is there any better way to binde this data without having to go through jQuery to bindearlo? I know it's a theme of the DefaultModelBinder. Would I have to create a Custom?