0

I am trying to extend ProDinner by adding phone numbers to Chef.

  1. ChefInput view model:

    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 view model:

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

       @using (Html.BeginForm())
       {
    
        @Html.TextBoxFor(o => o.Name)
        @Html.EditorFor(o => o.PhoneNumberInputs)
       }
    
  4. PhoneNumberInput.cshtml in EditorTemplate folder:

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

When debugging and I stop it at Create in Crudere file, the Phone collection is null.

Anyone have any ideas? Thanks in Advance.

Adam Lear
  • 38,111
  • 12
  • 81
  • 101
Joe Grasso
  • 465
  • 2
  • 5
  • 19
  • You have a complex very object in your ChefInput (List), viewmodels shouldn't have that, prodinner has nothing to do with this, you can create a one page app with this viewmodel and you'll get the same result – Omu May 24 '12 at 22:22
  • if you want to add many phone numbers to the chef you could have IEnumerable PhoneNumbers in your input, try to keep it as simple as possible, or if you need properties for the phone numbers (phonetype) then you should look here: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx – Omu May 24 '12 at 22:59

1 Answers1

1

Joe,

You don't show your controller logic but I've got a feeling you're getting null because you're not populating the PhoneNumberInputs ViewModel. From what I can see, all you're doing is newing up the list in the model. Ensure that you fill this 'list' in your controller from the database (with the appropriate values) and i'm certain all will work as planned.

[edit] - in answer to comment. don't know what the prodinner controllers etc look like but something alsong these lines:

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

as i said, not sure of the prodinner setup, but this is what i meant.

jim tollan
  • 22,305
  • 4
  • 49
  • 63
  • I am using the std Chef Controller ProDinner has, can you give me an example of what you mean by: "populating the PhoneNumberInputs ViewModel" ? – Joe Grasso May 24 '12 at 19:48
  • joe- made edit above. might be a little different to th eprodinner repository, so you'll have to think around it a little perhaps – jim tollan May 24 '12 at 20:27
  • Thanks Jim. all Crud is at a central loc (crudere) cannot make such methods in controller.And, the folks at ProDinner do not seem too keen to offer advice, their one sentence comment to my request was: "Post your question on StackOverflow." I am sorry I invested so much time in their product. – Joe Grasso May 24 '12 at 20:30
  • joe - that's a pain. maybe you should look at further examples as the centric idea sounds a bit limiting. will see if i can 'think' of any and add them to answer – jim tollan May 24 '12 at 20:35
  • @JoeGrasso you can do any methods in the controller, you can override methods if you need to (make them virtual if they aren't) – Omu May 24 '12 at 22:14
  • Chuck: I just looked up your profile. You are the author or prodinner. Instead of offering some code you send me a link. Tell you what: Do not count on my buying your product if this is a sign of your customer support. – Joe Grasso May 25 '12 at 02:58
  • @JoeGrasso nobody knows better than you what you want to achieve, we dont know why you are trying to do it this way, so I adviced to do it simpler. prodinners main purpose is to show mvc best practices, the way you built your input is not best practices at all – Omu May 25 '12 at 07:30
  • @Chuck: I accept that as fair criticism. However, it is one thing to say "You are not doing it correctly", vs. "Here is how I would do it." I do not think a link to a 2008 post, which I must admit I am having difficulty understanding, is helpful. As far as not explaining myself: What I am trying to accomplish, I think I made it very clear. I merely ask for sample code if when a Chef is created, multiple inputs are available for his phone # as well as a drop down for phone type. Look Chuck, I do not knee jerk to post here, I try to solve it on my own. Either you can help me w/ samples or not. – Joe Grasso May 25 '12 at 14:22
  • @JoeGrasso the link that I gave you is what you need, it's called list binding, there's a sample project available for download. The best way to understand it is to start from an empty mvc project and try to use the list binding. read the article carefully – Omu May 25 '12 at 20:30
  • @JoeGrasso I use it here (http://demo.aspnetawesome.com/ListBinding) to edit 3 rows at once, however, in your case it's harder because you don't have a fixed amount of phone numbers per chef, you could simplify it by making it fixed, or else you would have to generate html with js when user click addphone, the generated html would have to have [index] in the input's name (exactly like in Phil's article) I will add something in the new version of prodinner to show this technique, but for now I recommend you to read that article and try out in VS everything that is shown there – Omu May 25 '12 at 20:37