Questo è il mio primo progetto C#/MVC e ho problemi di associazione a un modello. Ho letto un Phil Haack's Post applicato e ho utilizzato EditorFor per tutte le mie viste parziali. Ho bisogno di un Model Binder personalizzato? Si prega di aiutareMVC C# Binding automatico del modello negli elenchi nidificati
In breve, ho una lista di settimane che contengono un elenco di voci. Queste voci contengono un elenco di ore
Azione:
[HttpPost]
public ActionResult SubmitRecords(List<WeekCollection> itemData)
{
//do stuff
return View();
}
Modello:
public class WeekCollection
{
public WeekCollection()
{
this.OneWeek = new List<Entry>();
}
public List<Entry> OneWeek { get; set; }
}
[Bind(Exclude = "Task, Project")]
public class Entry
{
public int ProjectId { get; set; }
public virtual Projects Project { get; set; }
public int TaskId { get; set; }
public virtual Tasks Task { get; set; }
public bool Billable { get; set; }
public List<Hours> Gethours { get; set; }
}
public class Hours
{
public float NumberOfHours { get; set; }
}
Vista (Indice)
//within partial view iteration with incrementing u (u++)
@using(@Html.BeginForm())
{
@Html.EditorFor(m => m[u].OneWeek, "TimesheetWeek")
<input type="Submit">
}
Vista (TimesheetWeek)
@foreach (var value in Model)
{
v++;
if (Model.All(x => x.projectId == 0))
{
@Html.DropDownListFor(p => p[v].projectId, (IEnumerable<SelectListItem>)projectList, "Select Project", new { @class = "notSelect" })
@Html.DropDownListFor(t => t[v].taskId, (IEnumerable<SelectListItem>)taskList, "Select Task", new { @class = "notSelect" })
}
else
{
if (value.projectId != 0)
{
@Html.DropDownListFor(p => p[v].projectId, (IEnumerable<SelectListItem>)projectList, new Dictionary<string, Object> { { "class", "SelectDrop" }, { "data-selectHead", value.projectId } })
@Html.DropDownListFor(t => t[v].taskId, (IEnumerable<SelectListItem>)taskList, new Dictionary<string, Object> { { "class", "SelectDrop" }, { "data-selectHead", value.taskId } })
}
}
@Html.CheckBoxFor(b => b[v].billable)
@Html.EditorFor(h => h[v].gethours, "HoursDisplay")
@value.gethours.Sum(a => a.numberOfHours)
}
View (HoursDisplay)
@for (var i = 0; i < Model.Count(); i++)
{
@Html.TextBoxFor(m => m[i].numberOfHours)
}
modello visualizza tutti i dati in modo corretto e l'uscita di dati del modulo pubblicato è il seguente:
[0].OneWeek.[0].projectId:1
[0].OneWeek.[0].taskId:1
[0].OneWeek.[0].billable:true
[0].OneWeek.[0].billable:false
[0].OneWeek.[0].gethours.[0].numberOfHours:0
[0].OneWeek.[0].gethours.[1].numberOfHours:5
[0].OneWeek.[0].gethours.[2].numberOfHours:7
[0].OneWeek.[0].gethours.[3].numberOfHours:6
[0].OneWeek.[0].gethours.[4].numberOfHours:4
[0].OneWeek.[0].gethours.[5].numberOfHours:8
[0].OneWeek.[0].gethours.[6].numberOfHours:0
ho pensato ho avuto il diritto di indicizzazione ma attualmente ottengo un oneweek vuoto nell'azione. Che cosa sto facendo di sbagliato? Qualsiasi assistenza è apprezzata. (Alcune ripetizioni e HTML sono stati rimossi)
Questo ha funzionato perfettamente grazie. Non mi sono reso conto di quanto sia utile l'EditorFor e di conseguenza i modelli di visualizzazione sono molto più semplici. Conoscenza molto utile. Grazie + Buon Natale – user1925048
Qual è il codice per Entry.cshtml? – Apollo