Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc ASP.NET核心MVC-添加DB绑定复选框以创建.cshtml_Asp.net Mvc_Entity Framework_Asp.net Core - Fatal编程技术网

Asp.net mvc ASP.NET核心MVC-添加DB绑定复选框以创建.cshtml

Asp.net mvc ASP.NET核心MVC-添加DB绑定复选框以创建.cshtml,asp.net-mvc,entity-framework,asp.net-core,Asp.net Mvc,Entity Framework,Asp.net Core,我正在使用ASP.NET Core MVC,试图添加一个复选框列表,这些复选框绑定到数据库中的对象列表,但我迷路了。在我的Create.cshtml页面中,路由显然是空的,那么我如何用数据库中我的所有捐赠者的列表填充all捐赠者属性,并在复选框列表中显示它们呢 路线等级: public class Route : ModelBase { [Key] public int RouteID { get; set; } public string Name { get; se

我正在使用ASP.NET Core MVC,试图添加一个复选框列表,这些复选框绑定到数据库中的对象列表,但我迷路了。在我的Create.cshtml页面中,路由显然是空的,那么我如何用数据库中我的所有捐赠者的列表填充all捐赠者属性,并在复选框列表中显示它们呢

路线等级:

public class Route : ModelBase
{
    [Key]
    public int RouteID { get; set; }

    public string Name { get; set; }

    [Display(Name = "Frequency")]
    public int FrequencyID { get; set; }
    public virtual Frequency Frequency { get; set; }

    [Display(Name = "DayOfWeek")]
    public int DayOfWeekID { get; set; }
    public virtual DayOfWeek DayOfWeek { get; set; }

    [Display(Name = "TimeOfDay")]
    public int TimeOfDayID { get; set; }
    public virtual TimeOfDay TimeOfDay { get; set; }

    [Display(Name = "Donors")]
    public int DonorID { get; set; }
    public ICollection<RouteOrg> RouteOrgDonors { get; set; }

    [Display(Name = "Agency")]
    public int AgencyID { get; set; }
    public ICollection<RouteOrg> RouteOrgAgencies { get; set; }

    public IEnumerable<Organization> AllDonors { get; set; }
    public IEnumerable<Organization> AllAgencies { get; set; }
}

如果我正确理解你的问题,你可以试试这个

@for (var i = 0; i < Model.AllDonors.Count; i++)
        {
var isChecked= = "";
if (Model.RouteOrgDonors.Contains(item)){

isChecked = "checked"
<input type="hidden" name="AllDonors[i].Id" 
    value="@AllDonors[i].Id"/>
    }
else {
 isChecked = "";
}    

<div class="checkbox">
        <label>
            <input type="checkbox"                    
                name="SelectedFruits"
                    value="Model.AllDonors[i].OrgID" />
                    Model.AllDonors[i].Name
                   @isChecked
         </label>
</div>       
        }
为了使用Count方法,您需要对All捐赠者属性使用IList而不是IEnumerable

public IList<Organization> AllDonors { get; set; }
创建视图:

@model Demo.Models.Route
<div class="form-group">
    <label asp-for="DonorID" class="control-label"></label>
    @for (var i = 0; i < Model.AllDonors.Count; i++)
    {
        <div class="checkbox">
            <label>
                <input type="checkbox"
                       name="SelectedFruits"
                       value="@Model.AllDonors[i].OrgID" />
                @Model.AllDonors[i].Name
            </label>
        </div>
    }
</div>
public IList<Organization> AllDonors { get; set; }
public IActionResult Create()
    {

        var Route = new Route();
        Route.AllDonors = _context.Organizations.ToList();//populate AllDonors

        return View(Route);
    }
@model Demo.Models.Route
<div class="form-group">
    <label asp-for="DonorID" class="control-label"></label>
    @for (var i = 0; i < Model.AllDonors.Count; i++)
    {
        <div class="checkbox">
            <label>
                <input type="checkbox"
                       name="SelectedFruits"
                       value="@Model.AllDonors[i].OrgID" />
                @Model.AllDonors[i].Name
            </label>
        </div>
    }
</div>