C# 我使用视图组件创建表单的一部分,但模型绑定器无法绑定
“我的表单”的一部分包括许多下拉列表,并将它们填入数据库的“视图组件”部分 这是我的观点C# 我使用视图组件创建表单的一部分,但模型绑定器无法绑定,c#,asp.net-core,razor,asp.net-core-mvc,asp.net-core-2.1,C#,Asp.net Core,Razor,Asp.net Core Mvc,Asp.net Core 2.1,“我的表单”的一部分包括许多下拉列表,并将它们填入数据库的“视图组件”部分 这是我的观点 @page @model Site.Web.Pages.Admin.CourseManagement.CreateModel <div class="row"> <form method="post" asp-page="/Admin/CourseManagement/Create" enctype="multipart/form-data"> <di
@page
@model Site.Web.Pages.Admin.CourseManagement.CreateModel
<div class="row">
<form method="post" asp-page="/Admin/CourseManagement/Create" enctype="multipart/form-data">
<div class="well col-md-7 col-sm-7">
<div class="form-group">
<div class="form-group">
<label asp-for="Model.CourseTitle" class="control-label"></label> :
<input asp-for="Model.CourseTitle" type="text" class="form-control" required />
<span asp-validation-for="Model.CourseTitle" class="text-danger"></span>
</div>
@await Component.InvokeAsync("FillCourseDetail")
<div class="form-group">
<label asp-for="Model.CoursePrice" class="control-label"></label> :
<input asp-for="Model.CoursePrice" class="form-control" min="1000" value="1000" placeholder="قیمت به ریال" type="number" required />
<span asp-validation-for="Model.CoursePrice" class="text-danger"></span>
</div>
<div class="form-group">
<span class="form-group glyphicon glyphicon-tags"> </span>
<input asp-for="Model.Keywordkeys" class="form-control " id="basic" data-role="tagsinput" placeholder="جداسازی تگ ها با کاما(,)" required>
</div>
</div>
<div class="form-group">
<input type="submit" class="btn glyphicon glyphicon-send" value="ذخیره اطلاعات" />
</div>
</div>
<div class="well col-md-5 col-sm-5">
<div class="form-group">
<div class="form-group">
<label asp-for="Model.UploadedImage" class="control-label"></label>
<div class="form-group">
<img class="img-responsive img-rounded" id="imgAvatar" src="~/images/UserProfile/index.png" />
</div>
<input asp-for="Model.UploadedImage" type="file" class="btn glyphicon glyphicon-open-file" required />
<span asp-validation-for="Model.UploadedImage" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Model.DemoFileName" class="control-label"></label>
<input asp-for="Model.DemoFileName" type="file" class="btn glyphicon glyphicon-open-file" required />
<span asp-validation-for="Model.DemoFileName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Model.CourseDescription" class="control-label"></label> :
<textarea asp-for="Model.CourseDescription" class="form-control total_area" type="text" required></textarea>
<span asp-validation-for="Model.CourseDescription" class="text-danger"></span>
</div>
</div>
</div>
</form>
</div>
@model CourseCreateVm
<div class="form-group">
<label asp-for="@Model.CourseGroupId" class="control-label"></label>
<select asp-for="@Model.CourseGroupId" class="form-control" asp-items="@(new SelectList(Model.CourseGroups, "Id", "Title"))" required><option class="text-danger">لطفا انتخاب کنید</option></select>
<span asp-validation-for="@Model.CourseGroupId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="@Model.CustomUserId" class="control-label"></label>
<select asp-for="@Model.CustomUserId" class="form-control" asp-items="@(new SelectList(Model.CustomUsers,"Id","ShowUserName"))" required><option class="text-danger">لطفا انتخاب کنید</option></select>
<span asp-validation-for="@Model.CustomUserId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="@Model.CourseLevelId" class="control-label"></label>
<select class="form-control" asp-for="@Model.CourseLevelId" asp-items="@(new SelectList(Model.CourseLevels, "Id", "Title"))"><option class="text-danger">لطفا انتخاب کنید</option></select>
<span asp-validation-for="@Model.CourseLevelId"></span>
</div>
<div class="form-group">
<label asp-for="@Model.CourseStatusId" class="control-label"></label>
<select class="form-control" asp-for="@Model.CourseStatusId" asp-items="@(new SelectList(Model.CourseStatuses, "Id", "Title"))" required><option class="text-danger">لطفا انتخاب کنید</option></select>
<span asp-validation-for="@Model.CourseStatusId"></span>
</div>
public class CourseCreateVm
{
public virtual int? CourseStatusId { get; set; }
public List<CourseStatusVm> CourseStatuses { get; set; }
public virtual int? CourseLevelId { get; set; }
public List<CourseLevelVm> CourseLevels { get; set; }
public virtual string CustomUserId { get; set; }
public List<CustomUserVm> CustomUsers { get; set; }
public virtual int? CourseGroupId { get; set; }
public List<CourseGroupVm> CourseGroups { get; set; }
public string CourseTitle { get; set; }
public string CourseDescription { get; set; }
public decimal CoursePrice { get; set; }
public IFormFile UploadedImage { get; set; }
public IFormFile DemoFileName { get; set; }
public List<string> Keywordkeys { get; set; }
}
@page
@模型Site.Web.Pages.Admin.CourseManagement.CreateModel
:
@wait Component.InvokeAsync(“FillCourseDetail”)
:
:
这是我的视图组件的视图
@page
@model Site.Web.Pages.Admin.CourseManagement.CreateModel
<div class="row">
<form method="post" asp-page="/Admin/CourseManagement/Create" enctype="multipart/form-data">
<div class="well col-md-7 col-sm-7">
<div class="form-group">
<div class="form-group">
<label asp-for="Model.CourseTitle" class="control-label"></label> :
<input asp-for="Model.CourseTitle" type="text" class="form-control" required />
<span asp-validation-for="Model.CourseTitle" class="text-danger"></span>
</div>
@await Component.InvokeAsync("FillCourseDetail")
<div class="form-group">
<label asp-for="Model.CoursePrice" class="control-label"></label> :
<input asp-for="Model.CoursePrice" class="form-control" min="1000" value="1000" placeholder="قیمت به ریال" type="number" required />
<span asp-validation-for="Model.CoursePrice" class="text-danger"></span>
</div>
<div class="form-group">
<span class="form-group glyphicon glyphicon-tags"> </span>
<input asp-for="Model.Keywordkeys" class="form-control " id="basic" data-role="tagsinput" placeholder="جداسازی تگ ها با کاما(,)" required>
</div>
</div>
<div class="form-group">
<input type="submit" class="btn glyphicon glyphicon-send" value="ذخیره اطلاعات" />
</div>
</div>
<div class="well col-md-5 col-sm-5">
<div class="form-group">
<div class="form-group">
<label asp-for="Model.UploadedImage" class="control-label"></label>
<div class="form-group">
<img class="img-responsive img-rounded" id="imgAvatar" src="~/images/UserProfile/index.png" />
</div>
<input asp-for="Model.UploadedImage" type="file" class="btn glyphicon glyphicon-open-file" required />
<span asp-validation-for="Model.UploadedImage" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Model.DemoFileName" class="control-label"></label>
<input asp-for="Model.DemoFileName" type="file" class="btn glyphicon glyphicon-open-file" required />
<span asp-validation-for="Model.DemoFileName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Model.CourseDescription" class="control-label"></label> :
<textarea asp-for="Model.CourseDescription" class="form-control total_area" type="text" required></textarea>
<span asp-validation-for="Model.CourseDescription" class="text-danger"></span>
</div>
</div>
</div>
</form>
</div>
@model CourseCreateVm
<div class="form-group">
<label asp-for="@Model.CourseGroupId" class="control-label"></label>
<select asp-for="@Model.CourseGroupId" class="form-control" asp-items="@(new SelectList(Model.CourseGroups, "Id", "Title"))" required><option class="text-danger">لطفا انتخاب کنید</option></select>
<span asp-validation-for="@Model.CourseGroupId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="@Model.CustomUserId" class="control-label"></label>
<select asp-for="@Model.CustomUserId" class="form-control" asp-items="@(new SelectList(Model.CustomUsers,"Id","ShowUserName"))" required><option class="text-danger">لطفا انتخاب کنید</option></select>
<span asp-validation-for="@Model.CustomUserId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="@Model.CourseLevelId" class="control-label"></label>
<select class="form-control" asp-for="@Model.CourseLevelId" asp-items="@(new SelectList(Model.CourseLevels, "Id", "Title"))"><option class="text-danger">لطفا انتخاب کنید</option></select>
<span asp-validation-for="@Model.CourseLevelId"></span>
</div>
<div class="form-group">
<label asp-for="@Model.CourseStatusId" class="control-label"></label>
<select class="form-control" asp-for="@Model.CourseStatusId" asp-items="@(new SelectList(Model.CourseStatuses, "Id", "Title"))" required><option class="text-danger">لطفا انتخاب کنید</option></select>
<span asp-validation-for="@Model.CourseStatusId"></span>
</div>
public class CourseCreateVm
{
public virtual int? CourseStatusId { get; set; }
public List<CourseStatusVm> CourseStatuses { get; set; }
public virtual int? CourseLevelId { get; set; }
public List<CourseLevelVm> CourseLevels { get; set; }
public virtual string CustomUserId { get; set; }
public List<CustomUserVm> CustomUsers { get; set; }
public virtual int? CourseGroupId { get; set; }
public List<CourseGroupVm> CourseGroups { get; set; }
public string CourseTitle { get; set; }
public string CourseDescription { get; set; }
public decimal CoursePrice { get; set; }
public IFormFile UploadedImage { get; set; }
public IFormFile DemoFileName { get; set; }
public List<string> Keywordkeys { get; set; }
}
@model CourseCreateVm
لطفا انتخاب کنید
لطفا انتخاب کنید
لطفا انتخاب کنید
لطفا انتخاب کنید
这是视图组件和主视图中使用的视图模型
@page
@model Site.Web.Pages.Admin.CourseManagement.CreateModel
<div class="row">
<form method="post" asp-page="/Admin/CourseManagement/Create" enctype="multipart/form-data">
<div class="well col-md-7 col-sm-7">
<div class="form-group">
<div class="form-group">
<label asp-for="Model.CourseTitle" class="control-label"></label> :
<input asp-for="Model.CourseTitle" type="text" class="form-control" required />
<span asp-validation-for="Model.CourseTitle" class="text-danger"></span>
</div>
@await Component.InvokeAsync("FillCourseDetail")
<div class="form-group">
<label asp-for="Model.CoursePrice" class="control-label"></label> :
<input asp-for="Model.CoursePrice" class="form-control" min="1000" value="1000" placeholder="قیمت به ریال" type="number" required />
<span asp-validation-for="Model.CoursePrice" class="text-danger"></span>
</div>
<div class="form-group">
<span class="form-group glyphicon glyphicon-tags"> </span>
<input asp-for="Model.Keywordkeys" class="form-control " id="basic" data-role="tagsinput" placeholder="جداسازی تگ ها با کاما(,)" required>
</div>
</div>
<div class="form-group">
<input type="submit" class="btn glyphicon glyphicon-send" value="ذخیره اطلاعات" />
</div>
</div>
<div class="well col-md-5 col-sm-5">
<div class="form-group">
<div class="form-group">
<label asp-for="Model.UploadedImage" class="control-label"></label>
<div class="form-group">
<img class="img-responsive img-rounded" id="imgAvatar" src="~/images/UserProfile/index.png" />
</div>
<input asp-for="Model.UploadedImage" type="file" class="btn glyphicon glyphicon-open-file" required />
<span asp-validation-for="Model.UploadedImage" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Model.DemoFileName" class="control-label"></label>
<input asp-for="Model.DemoFileName" type="file" class="btn glyphicon glyphicon-open-file" required />
<span asp-validation-for="Model.DemoFileName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Model.CourseDescription" class="control-label"></label> :
<textarea asp-for="Model.CourseDescription" class="form-control total_area" type="text" required></textarea>
<span asp-validation-for="Model.CourseDescription" class="text-danger"></span>
</div>
</div>
</div>
</form>
</div>
@model CourseCreateVm
<div class="form-group">
<label asp-for="@Model.CourseGroupId" class="control-label"></label>
<select asp-for="@Model.CourseGroupId" class="form-control" asp-items="@(new SelectList(Model.CourseGroups, "Id", "Title"))" required><option class="text-danger">لطفا انتخاب کنید</option></select>
<span asp-validation-for="@Model.CourseGroupId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="@Model.CustomUserId" class="control-label"></label>
<select asp-for="@Model.CustomUserId" class="form-control" asp-items="@(new SelectList(Model.CustomUsers,"Id","ShowUserName"))" required><option class="text-danger">لطفا انتخاب کنید</option></select>
<span asp-validation-for="@Model.CustomUserId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="@Model.CourseLevelId" class="control-label"></label>
<select class="form-control" asp-for="@Model.CourseLevelId" asp-items="@(new SelectList(Model.CourseLevels, "Id", "Title"))"><option class="text-danger">لطفا انتخاب کنید</option></select>
<span asp-validation-for="@Model.CourseLevelId"></span>
</div>
<div class="form-group">
<label asp-for="@Model.CourseStatusId" class="control-label"></label>
<select class="form-control" asp-for="@Model.CourseStatusId" asp-items="@(new SelectList(Model.CourseStatuses, "Id", "Title"))" required><option class="text-danger">لطفا انتخاب کنید</option></select>
<span asp-validation-for="@Model.CourseStatusId"></span>
</div>
public class CourseCreateVm
{
public virtual int? CourseStatusId { get; set; }
public List<CourseStatusVm> CourseStatuses { get; set; }
public virtual int? CourseLevelId { get; set; }
public List<CourseLevelVm> CourseLevels { get; set; }
public virtual string CustomUserId { get; set; }
public List<CustomUserVm> CustomUsers { get; set; }
public virtual int? CourseGroupId { get; set; }
public List<CourseGroupVm> CourseGroups { get; set; }
public string CourseTitle { get; set; }
public string CourseDescription { get; set; }
public decimal CoursePrice { get; set; }
public IFormFile UploadedImage { get; set; }
public IFormFile DemoFileName { get; set; }
public List<string> Keywordkeys { get; set; }
}
公共类课程创建VM
{
公共虚拟int?CourseStatusId{get;set;}
公共列表课程设置{get;set;}
公共虚拟int?CourseLevelId{get;set;}
公共列表CourseLevels{get;set;}
公共虚拟字符串CustomUserId{get;set;}
公共列表CustomUsers{get;set;}
公共虚拟整数?CourseGroupId{get;set;}
公共列表课程组{get;set;}
公共字符串CourseTile{get;set;}
公共字符串CourseDescription{get;set;}
公共十进制课程价格{get;set;}
公共文件上载映像{get;set;}
公共文件DemoFileName{get;set;}
公共列表关键字{get;set;}
}
但是当我提交表格的时候。下拉列表“模型绑定器”中的选定项无法将其绑定到模型
我应该如何解决此问题?尝试在视图组件中使用选择标记帮助的
名称属性,而不是asp,如下所示:
div class="form-group">
<label asp-for="@Model.CourseGroupId" class="control-label"></label>
<select name="CourseCreateVm.CourseGroupId" class="form-control" asp-items="@(new
SelectList(Model.CourseGroups, "Id", "Title"))" required><option class="text-danger">لطفا انتخاب کنید</option></select>
<span asp-validation-for="@Model.CourseGroupId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="@Model.CustomUserId" class="control-label"></label>
<select name="CourseCreateVm.CustomUserId" class="form-control" asp-items="@(new
SelectList(Model.CustomUsers,"Id","ShowUserName"))" required><option class="text-danger">لطفا انتخاب کنید</option></select>
<span asp-validation-for="@Model.CustomUserId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="@Model.CourseLevelId" class="control-label"></label>
<select name="CourseCreateVm.CourseLevelId" class="form-control" asp-items="@(new
SelectList(Model.CourseLevels, "Id", "Title"))"><option class="text-danger">لطفا انتخاب کنید</option></select>
<span asp-validation-for="@Model.CourseLevelId"></span>
</div>
<div class="form-group">
<label asp-for="@Model.CourseStatusId" class="control-label"></label>
<select name="CourseCreateVm.CourseStatusId" class="form-control" asp-items="@(new
SelectList(Model.CourseStatuses, "Id", "Title"))" required><option class="text-danger">لطفا انتخاب کنید</option></select>
<span asp-validation-for="@Model.CourseStatusId"></span>
</div>
div class=“表单组”>
لطفا انتخاب کنید
لطفا انتخاب کنید
لطفا انتخاب کنید
لطفا انتخاب کنید
谢谢。是的Work@MehdiSheykhveysi,我很高兴能帮你解决这个问题,你能把我的回答记下来吗?谢谢!