-1

I have a ViewModel and within there are a few lists. I want to show the following properties:

    public string Status { get; set; }
    public DateTime Applicationdate { get; set; }
    public DateTime Submitdate { get; set; }
    public Boolean CustomerAgreementBoolean { get; set; }
    public DateTime CustomerAgreementBooleanDate { get; set; }
    public string MultiannualContract { get; set; }

They are part of a bigger model. It half works when I do this in the view:

        @foreach (var offeritem in Model.Offermodel)
        {
           <div class="col-md-3 mb-3">
               @Html.LabelFor(model => model.Offermodel)
               @Html.EditorFor(x => offeritem.Applicationdate, new { 
               htmlAttributes = new { @class = "form-control", required = 
               "required" } })
               @Html.ValidationMessageFor(x => offeritem.Applicationdate, "", 
               new { @class = "text-danger" })
               </div>
           }

The names of the label and editor become: Offeritem.Applicationdate instead of "Applicationdate"

Is there any way to get the correct names to show?

Carlove
  • 429
  • 1
  • 4
  • 15
  • They are the correct names, as per the model that you have provided. The ApplicationDate is a property of OfferItem and so therefore that's the name it binds to. There may be a way around by writing your own custom model binders, but whats wrong with the name "Offeritem.Applicationdate"? – Wheels73 Jul 04 '18 at 13:50
  • When I pass the view back to the controller the model holds no data. I was suspecting this to be the culprit, since the names aren't the same anymore. – Carlove Jul 04 '18 at 13:53
  • I see. Out of interest, are the values posted ok if you use a TextBoxFor? I would have thought that the model binding would be the same on EditorFor and TextboxFor. – Wheels73 Jul 04 '18 at 13:55
  • TextboxFor doesn't work either, sadly. But now that I let my test run, I see other stuff are going wrong. The three dropdownlists I've placed say: 'null reference' upon sending the data. Then the model I pass as the parameter is empty. BUT when doing a ```ModelState.IsValid``` the dropdownlist data has been sent succesfully. Other data has not. Removing the dropdownlists from the view change nothing ( except for the modelstate stating there is not data now ) – Carlove Jul 04 '18 at 13:58
  • Ahh... I can see what it is. You can't use foreach for model binding.. You need a unique index. Change your loop to for int i = 0 etc... then say Model.OfferModel[i] and you should be ok. – Wheels73 Jul 04 '18 at 14:02

1 Answers1

0

The names of the nested properties will be rendered like this. You can change the name attribute by passing the attribute value in HTML helper like

 @Html.EditorFor(x => offeritem.Applicationdate, new { 
               htmlAttributes = new { @class = "form-control", required = 
               "required", name="Applicationdate" } })

But then it would not bind to the nested property on form post.

Manvinder Singh
  • 140
  • 1
  • 9
  • You should try your code first - if you did you would see that it does nothing at all (which is fortunate because changing the `name` attribute would screw up model binding) –  Jul 04 '18 at 21:56
  • By default strong bind will automatically generate name, id and class behind the scene. You can see through inspect it. it will generate like that. offeritem_Applicationdate – Asif Raza Jul 05 '18 at 05:32