У меня проблемы со сложной формой в моем бэк-офисе.
Когда форма отправляется на сервер в [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; }
}