0

模型绑定器无法在我的操作参数中实例化子对象 -PublicationSchedule始终为空,, ,Id填充正常。谁能建议这可能是为什么?NameRegion

那个行动:

[HttpPost]
public ActionResult Post(ProductTemplateViewModel viewModel)
{
    //...
}

要绑定的模型:

public class ProductTemplateViewModel
{
    [Required]
    public string Id { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string Region { get; set; }
    [Required] 
    public PublicationScheduleViewModel PublicationSchedule { get; set; }
}

public class PublicationScheduleViewModel
{
    [Required, StringArrayNotEmpty]
    public string[] DaysOfWeek { get; set; }
    [Required, Range(0, 23)]
    public int HoursOfDay { get; set; }
    [Required, Range(0, 59)]
    public int MinutesOfHour { get; set; }
}

HTTP 请求表单数据:

id:gf
name:fg
region:Africa
daysOfWeek:Wednesday
hoursOfDay:3
minutesOfHour:4

表格:

<input type="text" id="id" name="id">
<input type="text" id="name" name="name">
<select id="region" name="region">
    <option value="Africa" selected="selected">Africa</option>
</select>       
<input type="checkbox" id="chk(Monday)" name="daysOfWeek" value="Monday">
<input type="checkbox" id="chk(Tuesday)" name="daysOfWeek" value="Tuesday">
<select id="hoursOfDay" name="hoursOfDay">
        <option value="0">00</option>
    <option value="1">01</option>                       
</select>
<select id="minutesOfHour" name="minutesOfHour">
    <option value="0" selected="selected">00</option>
    <option value="1">01</option>
    <option value="2">02</option>                       
</select>
4

1 回答 1

2

字段名称需要为 PublicationSchedule.daysOfWeek、PublicationSchedule.hoursOfDay 和 PublicationSchedule.minutesOfHour

于 2012-07-11T13:26:16.223 回答