aspnet / Mvc

[Archived] ASP.NET Core MVC is a model view controller framework for building dynamic web sites with clean separation of concerns, including the merged MVC, Web API, and Web Pages w/ Razor. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
5.62k stars 2.14k forks source link

Issues with value attribute not being set #8775

Closed MCFHTAGENTS closed 5 years ago

MCFHTAGENTS commented 5 years ago

I have the following CSHTML:

<input asp-for="Customer.LastName" class="form-control" />
@Html.EditorFor(a => a.Customer.LastName)
Last Name: @(Model.Customer.LastName)

which is rendered as:

<input class="form-control" type="text" data-val="true" data-val-maxlength="The field Last Name must be a string or array type with a maximum length of '25'." data-val-maxlength-max="25" id="Customer_LastName" name="Customer.LastName" value="">
<input class="text-box single-line" id="Customer_LastName" name="Customer.LastName" type="text" value="">
LastName: Bean

Customer is defined as public CustomerCreate Customer { get; set; }

and CustomerCreate, partially, as

    public class CustomerCreate
    {

        [Display(Name = "QB_LASTNAME")]
        [RequiredFromStage("CustomerFieldNeeded", 1, ErrorMessage = "ERROR_QB_SURNAME_REQUIRED")]
        [MaxLength(25)]
        public string LastName { get; set; }
    }
}

As you will see the value attribute is not being set for the first two but is present in the POCO object Customer (which is a POCO object of type CustomerCreate - all mine not MVC)

There are no Editor Templates in play for strings.

This is in MVC Core 2.1.6

Has anyone any ideas?

Thanks Mark

mkArtakMSFT commented 5 years ago

Thanks for contacting us, @MCFHTAGENTS. @NTaylorMullen, is the behavior described here by design?

NTaylorMullen commented 5 years ago

As you will see the value attribute is not being set for the first two but is present in the POCO object Customer (which is a POCO object of type CustomerCreate - all mine not MVC)

How are you setting LastName? Via controller? Form post?

MCFHTAGENTS commented 5 years ago

Both in a way!

The form starts off as a view served from a HttpGet method on a controller. At this point Customer is null.

The user then submits the form a number of times (think of a wizard design pattern) incrementing a page counter passed back and forth as a hidden value. As they navigate through (effectively, create a booking, enter some key customer attributes, searching for an existing customer and if found direct that this record be used otherwise create a new one). In the use case here we have searched for a record and are creating a new one so the Customer record is populated with a skeleton record in the controller that is not persisted to the database at that point but used to pass the values to the view.

Thanks Mark

NTaylorMullen commented 5 years ago

@MCFHTAGENTS could you provide a small repro without a database?

MCFHTAGENTS commented 5 years ago

TestChildPOCO.zip picture1

The attached zip file creates the output on my machine as presented by the attached image

NTaylorMullen commented 5 years ago

@MCFHTAGENTS you're not @addTagHelpering the Mvc.TagHelpers so the input tag helper isn't running. For example, if you view source: image Note the asp-for is still in the output.

However, if at the top of your file you add @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers Everything works as expected: image