Веб-приложение ASP.NET Core 2.0 Razor Pages отдельная сложная форма BindProperty - PullRequest
0 голосов
/ 22 мая 2018

Я новичок в фреймворке веб-приложений ASP.NET Core 2.0 Razor Pages.Проблема заключается в следующем: мне нужно создать HTML-форму для сложного [BindProperty] класса и отделить его сложные поля в частичных представлениях или редакторах (как в MVC):

public class BasicInformation
{
    // complex filed
    public Name Name { get; set; }

    public string Email { get; set; }

    // collection of objects
    public IEnumerable<Address> Addresses { get; set; }

    //complex field
    public PhoneNumber PhoneNumber { get; set; }

}

Я хотел бы добиться чего-то вродеэто:

  <h4>BasicInformation</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <input type="hidden" asp-for="Resume" />

                @Html.EditorFor(m => m.BasicInformation.Name)
            <div class="form-group">
                <label asp-for="BasicInformation.Email" class="control-label"></label>
                <input asp-for="BasicInformation.Email" class="form-control" />
                <span asp-validation-for="BasicInformation.Email" class="text-danger"></span>
            </div>
            <div class="form-group">
                @Html.EditorFor(m => m.BasicInformation.PhoneNumber)
            </div>

           ...
            <div class="form-group">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </form>
    </div>
</div>`

И модель моей страницы:

 public class BasicInformationFormModel : PageModel
    {
        [BindProperty]
        public BasicInformation BasicInformation { get; set; }

        public IActionResult OnGet()
        {
            return Page();
        }

        public IActionResult OnPost(BasicInformation basicInformation)
        {
            // all data from separate views / editors need to be present in basicInformation
            return Page();
        }
    }

Как мне этого добиться?

1 Ответ

0 голосов
/ 22 мая 2018

Вам необходимо создать класс модели, чтобы деформировать модель BasicInformation,

public class ProfileViewModel{
       public BasicInformation BasicInformation {get;set;}
}

, а затем сделать просмотр как строго типизированный с моделью ProfileViewModel

@model ProfileViewModel
...