0

我正在尝试通过向 Chef 添加电话号码来扩展 ProDinner。

  1. ChefInput 视图模型:

    public class ChefInput :Input
    {  
        public string Name { get; set; }
    
        public ChefInput()
        {
            PhoneNumberInputs = new List<PhoneNumberInput>(){
                                new PhoneNumberInput()
                            };}
    
        public IList<PhoneNumberInput> PhoneNumberInputs { get; set; }
    }
    
  2. PhoneInput 视图模型:

    public class PhoneNumberInput :Input
    {
        public string Number { get; set; }
        public PhoneType PhoneType { get; set; } <-- an enum in Core project
    }
    
  3. 厨师 Create.cshtml 文件:

       @using (Html.BeginForm())
       {
    
        @Html.TextBoxFor(o => o.Name)
        @Html.EditorFor(o => o.PhoneNumberInputs)
       }
    
  4. EditorTemplate 文件夹中的 PhoneNumberInput.cshtml:

    @using (Html.BeginCollectionItem("PhoneNumberInputs"))
    {
        @Html.DropDownListFor(m => m, new SelectList(Enum.GetNames(typeof(PreDefPhoneType)))) 
        @Html.TextBoxFor(m => m.Number)
    }
    

调试时,我在 Crudere 文件中的 Create 处停止它,Phone 集合为空。

有人有想法么?提前致谢。

4

1 回答 1

1

乔,

您没有显示您的控制器逻辑,但我有一种感觉,因为您没有填充PhoneNumberInputsViewModel,所以您正在获取 null。据我所知,您所做的只是更新模型中的列表。确保您从数据库(使用适当的值)在您的控制器中填写此“列表”,并且我确信一切都会按计划工作。

[编辑] - 回答评论。不知道 prodinner 控制器等是什么样的,但也有以下几行:

public ActionResult Edit(int id)
{
    var viewModel = new ChefInput();
    viewModel.ChefInput =  _context.GetById<ChefModel>(id);
    viewModel.PhoneNumberInputs = _context.All<PhoneNumberInput>();
    return View(viewModel);
}

正如我所说,不确定 prodinner 设置,但这就是我的意思。

于 2012-05-24T19:27:09.447 回答