mcintyre321 / FormFactory

MVC5, Core or standalone - Generate rich HTML5 forms from your ViewModels, or build them programatically
http://formfactoryaspmvc.azurewebsites.net/
MIT License
304 stars 103 forks source link

Enums are not initialized with - selected="selected" #74

Closed morten-hogseth closed 5 years ago

morten-hogseth commented 5 years ago

Looking at the documentation on the "Enum" section. I am unable to get this to work using the same "Enum Example" class.

Other parts works as expected, and both the C# part and front-end stuff are embedded into the project. I am referencing all the required assets in the .cshtml file.

`

`

This is something that should be set by the form renderer right? @FF.PropertiesFor(Model).Render(Html)

And not by any js script.

Any suggestions to what might cause this behaviour?

mcintyre321 commented 5 years ago

I take it the model has the property set to an enumeration value?

Are you on Asp.net MVC classic or dot net core?

Do the example projects work on your machine?

On Sun, 13 Jan 2019, 17:57 Morten Høgseth <notifications@github.com wrote:

Looking at the documentation on the "Enum" section. I am unable to get this to work using the same "Enum Example" class.

Other parts works as expected, and both the C# part and front-end stuff are embedded into the project. I am referencing all the required assets in the .cshtml file.

This is something that should be set by the form renderer right? @FF.PropertiesFor(Model).Render(Html)

And not by any js script.

Any suggestions to what might cause this behaviour?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/mcintyre321/FormFactory/issues/74, or mute the thread https://github.com/notifications/unsubscribe-auth/AAQ0-qMvn6o2zJylYR5rjhhgpdat3mp-ks5vC3OkgaJpZM4Z9Srk .

morten-hogseth commented 5 years ago

Yes

ASP.NET Core

I will download the test projects and see if those work.

morten-hogseth commented 5 years ago

I downloaded the source, my observations are:

AspMvc.Example => Enums are initially set AspNetCore.Example => Enums are NOT initially set

Could you investigate what is necessary to make this work on AspNetCore?

mcintyre321 commented 5 years ago

Looks like a similar issue to the one fixed in https://github.com/mcintyre321/FormFactory/pull/64

I think FormFactory/Views/Shared/FormFactory/Property.System.Enum.cshtml needs to be changed from

@using FormFactory.AspMvc
@using FormFactory
@model PropertyVm
@{
    var choices = (IEnumerable<Tuple<string, object>>)Model.Choices ?? Model.Type.GetChoicesForEnumType();
}
@if (Model.Readonly)
{
    <input type="text"  class="form-control" name="@Model.Name" @Html.Raw(Model.Readonly()) @Html.Raw(Model.Disabled()) value="@Model.Value" />
}
else
{
    <select class="form-control" name="@Model.Name" @Html.Raw(Model.Readonly()) @Html.Raw(Model.Disabled())>
        @if (Nullable.GetUnderlyingType(Model.Type) != null)
        {
            <option value="" @Html.Raw((Model.Value == null).Attr("selected"))></option>
        }
        @foreach (var choice in choices)
        {
            var selected = (Model.Value != null && Model.Value.ToString() == choice.Item2.ToString());
            <option value="@choice.Item2" @Html.Raw(selected.Attr("selected"))>@choice.Item1</option>
        }
    </select>
}

to

@using FormFactory.AspMvc
@using FormFactory
@model PropertyVm
@{
    var choices = (IEnumerable<Tuple<string, object>>)Model.Choices ?? Model.Type.GetChoicesForEnumType();
}
@if (Model.Readonly)
{
    <input type="text"  class="form-control" name="@Model.Name" @Html.Raw(Model.Readonly()) @Html.Raw(Model.Disabled()) value="@Model.Value" />
}
else
{
    <select class="form-control" name="@Model.Name" @Html.Raw(Model.Readonly()) @Html.Raw(Model.Disabled())>
        @if (Nullable.GetUnderlyingType(Model.Type) != null)
        {
             var selected = Model.Value == null;
            <option value="" selected="@selected"></option>
        }
        @foreach (var choice in choices)
        {
            var selected = (Model.Value != null && Model.Value.ToString() == choice.Item2.ToString());
            <option value="@choice.Item2" selected="@selected">@choice.Item1</option>
        }
    </select>
}

Would you be able to try those changes out, and see if they still work in both Example projects, and send me a Pull Request?

Sorry, I don't have much time for working on this.

morten-hogseth commented 5 years ago

Sure thing. Thanks