如何使用剃须刀页面从局部视图发布数据

时间:2020-06-22 01:09:50

标签: .net-core partial-views razor-pages

我正在一个使用剃须刀页面作为UI的项目中。我目前在部分视图中发布数据时遇到问题,我的代码如下所示

模型

public class CompanyDto
{
   public string Name {get;set}
   public Availability TradingHours {get;set;}
}

public class Availability
{
   public long Id {set; get;}
   public List<AvailablityTime> AvailabilityTimes {get;set;}
}

public class AvailabilityTime
{ 
  public short DayOfWeek {get; set;}
}

剃须刀背后的页面代码

 public class CreateModel : BasePageModel
    {
        [BindProperty]        
        public CompanyDto command { get; set; }
        
         public IActionResult OnGet()
        {
            command = new CompanyDto ();
            command.TradingHours = new Availability();
            
            foreach (var day in Enum.GetValues(typeof(DayOfWeek)).OfType<DayOfWeek>().ToList())
            {
                command.TradingHours.AvailabilityTimes.Add(new AvailabityTimeVm
                {
                    DayOfWeek = (short)day
                });
            }

            return Page();
        }

创建页面

@page "/Company/Create"
@model Dashteq.Droplets.Clients.Razor.Pages.Company.CreateModel
@{
    ViewData["Title"] = "Create";
    Layout = "~/Pages/Shared/_CustomThemeLayout.cshtml";
}

<h4>Create</h4>
<hr />
<div class="row">
    <div class="col">
        <form method="post" enctype="multipart/form-data">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="row">
                <div class="form-group col-md-6">
                    <label asp-for="command.Name" class="control-label"></label>
                    <input asp-for="command.Name" class="form-control" />
                    <span asp-validation-for="command.Name" class="text-danger"></span>
                </div>
       <div>
<partial name="_AvailabilityPartial" for="command.TradingHours"/>
<input type="submit" />
</div>
</div>
</form>
</div>
</div>


Partial View
@model AvailabilitiesVm
    <ul class="list-unstyled">
        <div class="col-md-8 offset-4 row">
            <div class="col-md-6">
                <h5>Opening</h5>
            </div>
            <div class="col-md-6">
                <h5 class="text-sm-left">Closing</h5>
            </div>
        </div>
        @foreach (var item in Model.AvailabilityTimes)
        {
            <li>
                <div class="row ml-2">
                    <div class="col-md-4">
                        @Html.DisplayFor(model => item.DayOfWeekName)
                    </div>
                </div>
            </li>
        }
    </ul>

当我单击提交按钮时,CompanyDto的TradingHours字段为空。关于如何将部分视图中的数据获取到bound属性中的任何想法?

0 个答案:

没有答案