我在后台遇到复杂的表格。
当通过[HTTP POST]将表格发送到服务器时,我的模型是错误的。
即使我选中复选框,我的模型也总是返回false。
希望你们能帮助我,我在这个问题上浪费了很多时间,却一无所获。
我的模型的一部分
<!-- language: lang-csharp -->
public class project{
......
public List<Skill> Skills { get{....} set{} }
public class Skill
{
public int SkillId { get; set; }
public string SkillName { get; set; }
public string SkillNumber { get; set; }
public bool IsChecked { get; set; }
public HttpPostedFileBase files { get; set; }
}
我的观点
<!-- language: lang-razor -->
for (int i = 0; i < Model.Skills.Count; i++)
{
<div class="form-group row">
<div class="col-8">
@Html.CheckBoxFor(model => model.Skills[i].IsChecked)
@Html.LabelFor(model => model.Skills[i].IsChecked, Model.Skills[i].SkillName)
</div>
<div class="col-4">
<input type="hidden" name="@Model.Skills[i].SkillId" id="@Model.Skills[i].SkillId" />
</div>
</div>
}
我的控制器的一部分
<!-- language: lang-csharp -->
foreach (Models.Skill skill in viewModel.Skills)
{
if (skill.IsChecked)
{
if(skill.files.ContentLength > 0)
{
string fileName = skill.SkillNumber;
string filepath = (path + "/" + fileName);
skill.files.SaveAs(filepath);
}
}
}
如果你们需要更多信息,别忘了问。
已更新:
剃刀-cshtml视图
@using (Html.BeginForm())
{
<div class="form-group">
@Html.LabelFor(model => model.ProjectTitle)
@Html.TextBoxFor(model => model.ProjectTitle, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ProjectTitle, String.Empty, new { @class = "form-text text-muted" })
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProjectDescriptionShort)
@Html.TextBoxFor(model => model.ProjectDescriptionShort, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ProjectDescriptionShort, String.Empty, new { @class = "form-text text-muted" })
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProjectDescriptionFull)
@Html.TextBoxFor(model => model.ProjectDescriptionFull, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ProjectDescriptionFull, String.Empty, new { @class = "form-text text-muted" })
</div>
<div class="form-group">
@Html.LabelFor(Model => Model.ProjectTypes)<br />
@Html.DropDownListFor(Model => Model.ProjectTypeId, Model.ProjectTypes, "Selectionnez une type de projet", new { @class = "form-control" })
@Html.ValidationMessageFor(Model => Model.ProjectTypes, String.Empty, new { @class = "form-text text-muted" })
</div>
for (int i = 0; i < Model.Skills.Count; i++)
{
<div class="form-control">
@Html.CheckBoxFor(model => model.Skills[i].IsChecked)
@Html.LabelFor(model => model.Skills[i].IsChecked, Model.Skills[i].SkillName)
</div>
}
<input type="submit" value="valider" />
}
控制器动作
[HttpPost]
public ActionResult Index(Models.Project viewModel)
{
if (ModelState.IsValid)
{
Models.User user = (Models.User)Session["User"];
Datas.DataSetProjectTableAdapters.tbProjectTableAdapter tbProjectTableAdapter = new Datas.DataSetProjectTableAdapters.tbProjectTableAdapter();
tbProjectTableAdapter.Insert(
viewModel.ProjectTitle
, viewModel.ProjectDescriptionShort
, viewModel.ProjectDescriptionFull
, user.UserId
, viewModel.ProjectTypeId
);
Datas.DataSetProject.tbProjectDataTable lastProjectEntry = tbProjectTableAdapter.GetDataByLastInsert();
int projectId = (int)lastProjectEntry.Rows[0]["Id"];
string path = Server.MapPath("~/App_Data/UploadedFiles/Project/" + viewModel.ProjectTitle); // Give the specific path
if (!(System.IO.Directory.Exists(path)))
{
System.IO.Directory.CreateDirectory(path);
}
else{}
foreach (Models.Skill skill in viewModel.Skills)
{
if (skill.IsChecked)
{
if(skill.files.ContentLength > 0)
{
... not implented yet
}
}
}
}
return View(viewModel);
}
查看模型c#
public class Project
{
[Required]
public string ProjectTitle { get; set; }
[Required]
public string ProjectDescriptionShort { get; set; }
[Required]
public string ProjectDescriptionFull { get; set; }
[Required]
public int ProjectTypeId { get; set; }
public IEnumerable<SelectListItem> ProjectTypes
{
get
{
Datas.DataSetProject.tbProjectTypeDataTable tbProjectTypes = new Datas.DataSetProjectTableAdapters.tbProjectTypeTableAdapter().GetData();
List<SelectListItem> Items = new List<SelectListItem>();
foreach(Datas.DataSetProject.tbProjectTypeRow row in tbProjectTypes)
{
Items.Add(new SelectListItem()
{
Value = row.Id.ToString(),
Text = row.ProjectName
});
}
return Items;
}
set { }
}
public List<Skill> Skills
{
get
{
Datas.DataSetProject.tbSkillDataTable tbSkills = new Datas.DataSetProjectTableAdapters.tbSkillTableAdapter().GetData();
List<Skill> Items = new List<Skill>();
foreach (Datas.DataSetProject.tbSkillRow row in tbSkills)
{
Items.Add(new Skill()
{
SkillId = row.Id,
SkillName = row.SkillNumber + " - " + row.SkillName,
SkillNumber = row.SkillNumber
});
}
return Items;
}
set { }
}
}
public class Skill
{
public int SkillId { get; set; }
public string SkillName { get; set; }
public string SkillNumber { get; set; }
public bool IsChecked { get; set; }
public HttpPostedFileBase files { get; set; }
}
答案 0 :(得分:0)
您可以尝试使用下面的代码代替CheckboxFor吗?
@ Html.CheckBox(“ IsChecked”,model.Skills [i] .IsChecked)
让我知道您尝试这个问题后是否仍然遇到问题。
答案 1 :(得分:0)
您应该发布所有的cshtml文件和http发布操作
剃刀-cshtml视图
@using (Html.BeginForm())
{
<div class="form-group">
@Html.LabelFor(model => model.ProjectTitle)
@Html.TextBoxFor(model => model.ProjectTitle, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ProjectTitle, String.Empty, new { @class = "form-text text-muted" })
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProjectDescriptionShort)
@Html.TextBoxFor(model => model.ProjectDescriptionShort, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ProjectDescriptionShort, String.Empty, new { @class = "form-text text-muted" })
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProjectDescriptionFull)
@Html.TextBoxFor(model => model.ProjectDescriptionFull, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ProjectDescriptionFull, String.Empty, new { @class = "form-text text-muted" })
</div>
<div class="form-group">
@Html.LabelFor(Model => Model.ProjectTypes)<br />
@Html.DropDownListFor(Model => Model.ProjectTypeId, Model.ProjectTypes, "Selectionnez une type de projet", new { @class = "form-control" })
@Html.ValidationMessageFor(Model => Model.ProjectTypes, String.Empty, new { @class = "form-text text-muted" })
</div>
for (int i = 0; i < Model.Skills.Count; i++)
{
<div class="form-control">
@Html.CheckBoxFor(model => model.Skills[i].IsChecked)
@Html.LabelFor(model => model.Skills[i].IsChecked, Model.Skills[i].SkillName)
</div>
}
<input type="submit" value="valider" />
}
控制器操作
[HttpPost]
public ActionResult Index(Models.Project viewModel)
{
if (ModelState.IsValid)
{
Models.User user = (Models.User)Session["User"];
Datas.DataSetProjectTableAdapters.tbProjectTableAdapter tbProjectTableAdapter = new Datas.DataSetProjectTableAdapters.tbProjectTableAdapter();
tbProjectTableAdapter.Insert(
viewModel.ProjectTitle
, viewModel.ProjectDescriptionShort
, viewModel.ProjectDescriptionFull
, user.UserId
, viewModel.ProjectTypeId
);
Datas.DataSetProject.tbProjectDataTable lastProjectEntry = tbProjectTableAdapter.GetDataByLastInsert();
int projectId = (int)lastProjectEntry.Rows[0]["Id"];
string path = Server.MapPath("~/App_Data/UploadedFiles/Project/" + viewModel.ProjectTitle); // Give the specific path
if (!(System.IO.Directory.Exists(path)))
{
System.IO.Directory.CreateDirectory(path);
}
else{}
foreach (Models.Skill skill in viewModel.Skills)
{
if (skill.IsChecked)
{
if(skill.files.ContentLength > 0)
{
... not implented yet
}
}
}
}
return View(viewModel);
}
查看模型c#
public class Project
{
[Required]
public string ProjectTitle { get; set; }
[Required]
public string ProjectDescriptionShort { get; set; }
[Required]
public string ProjectDescriptionFull { get; set; }
[Required]
public int ProjectTypeId { get; set; }
public IEnumerable<SelectListItem> ProjectTypes
{
get
{
Datas.DataSetProject.tbProjectTypeDataTable tbProjectTypes = new Datas.DataSetProjectTableAdapters.tbProjectTypeTableAdapter().GetData();
List<SelectListItem> Items = new List<SelectListItem>();
foreach(Datas.DataSetProject.tbProjectTypeRow row in tbProjectTypes)
{
Items.Add(new SelectListItem()
{
Value = row.Id.ToString(),
Text = row.ProjectName
});
}
return Items;
}
set { }
}
public List<Skill> Skills
{
get
{
Datas.DataSetProject.tbSkillDataTable tbSkills = new Datas.DataSetProjectTableAdapters.tbSkillTableAdapter().GetData();
List<Skill> Items = new List<Skill>();
foreach (Datas.DataSetProject.tbSkillRow row in tbSkills)
{
Items.Add(new Skill()
{
SkillId = row.Id,
SkillName = row.SkillNumber + " - " + row.SkillName,
SkillNumber = row.SkillNumber
});
}
return Items;
}
set { }
}
}
public class Skill
{
public int SkillId { get; set; }
public string SkillName { get; set; }
public string SkillNumber { get; set; }
public bool IsChecked { get; set; }
public HttpPostedFileBase files { get; set; }
}
答案 2 :(得分:0)
问题在于您的视图仅显示每个技能的复选框并为其分配值。它没有将复选框直接绑定到正确的模型属性。请参阅有关为集合创建显示模板的文章。这是您需要采取的方向。从长远来看,它更易于维护和重用。
ASP.net MVC - Display Template for a collection
此外,看起来您在视图模型中的技能获取者只会返回一个全新的列表。这可能是控制器每次都获取所有错误的新列表的原因。尝试改为在视图模型的构造函数中构建列表,并使用默认的getter和setter。
答案 3 :(得分:0)
有时 CheckboxFor 有一些匿名错误,请使用以下代码作为建议。
<input type="hidden" name="@model.Skills[i].SkillId" />
<input type="checkbox" name="@model.Skills[i].IsChecked" />