0

My baldness is growing more rapidly than it should be. I first posted this question a couple days ago. I now know the problem and have it working... sort of. Another problem surfaced in it's place.

To solve the previous problem, I manually created the name to requestedDays[{0}].DateOfLeave where {0} was a Guid. This allowed my controller to properly receive the List<> of values.

Using this article's method, the name generated is requestedDays[{0}].DayRequested.DateOfLeave which my controller doesn't properly receive because the name has the class in it, DayRequested.DateOfLeave.

    [Authorize, HttpPost]
    public ActionResult Create(LeaveRequest leaveRequest, List<DayRequested> requestedDays)
    {
    }

I have tried to figure out work-arounds with the manual generation, but nothing I have tried works thus far. You can see my validation method here. I do know about the second part of Sanderson's article on validation however, it is quite hard to validate something that isn't being passed into the method.

This is my ViewModel I am using in my partial view.

public class LeaveRequestRow
{
    public LeaveRequestRow(DayRequested dayRequested, List<SelectListItem> leaveRequestType)
    {
        this.DayRequested = dayRequested;
        this.LeaveRequestType = leaveRequestType;
    }

    public List<SelectListItem> LeaveRequestType { set; get; }
    public DayRequested DayRequested { set; get; }
}

Does anyone have any ideas on how to proceed? Should I convert my dropdown to a jQuery build control and stop using the ViewModel?

Community
  • 1
  • 1
Mike Wills
  • 20,959
  • 28
  • 93
  • 149

2 Answers2

0

Binding 1-N controller arguments of complex types can be kind of tricky.

Your code examples are not meshing with my fried end of day Friday brain but I'll give it a shot.

Assuming the LeaveRequest class looks like this:

public class LeaveRequest {
    public string Text { get; set; }
    public string Number { get; set; }
}

The posted form keys must be:

leaveRequest.Text
leaveRequset.Number

That is the easy part. The 1-N binding of a list of DayRequested gets a little weird. Say the DayRequested object looks like this:

public class DayRequested  {
    public string Words { get; set; }
    public string Data { get; set; }
}

Your posted form keys look like:

requestedDays[0].Data
requestedDays[0].Words
requestedDays[1].Data    
requestedDays[1].Words
requestedDays[2].Data    
requestedDays[2].Words
requestedDays[3].Data    
requestedDays[3].Words

The default MVC binder should then trun all 10 form values into your two method arguments ... a POCO and a List of POCOs.

ZaChickster
  • 430
  • 1
  • 8
  • 14
  • I am confused. My posted keys are `requestedDays[x].Class.Field` right now. I want to change that to `requestedDays[x].Field` without needing to manually rename everything. – Mike Wills Nov 22 '10 at 16:58
0

I have solved this, though not as elegantly as I had hoped. All TextBoxFor had to be changed to TextBox along with the addtional changes needed with doing this. The names then were correctly generated and I could move on. This did break the ability for the validation message to appear next to the field, though ValidationSummary still does work. I will be working on fixing that later on and post code samples and a solution on my website.

Mike Wills
  • 20,959
  • 28
  • 93
  • 149