正如我在 ViewModel 中的回发列表 (MySalonTreatments) 属性为空后的标题中提到的。
我是 MVC 的新手,但我认为它应该可以工作。模型绑定(bind)器知道如何处理复杂类型。 我进行了很多搜索,但没有找到任何解决方案。也许你可以指出我的错误。
View 模型:
public class CreateSalonViewModel
{
public string Name { get; set; }
[DataType(DataType.EmailAddress)]
public string EmailAddress { get; set; }
//it shouldn't be facebook adress
public string Website { get; set; }
public string MobilePhone { get; set; }
public string LandlinePhone { get; set; }
public int CityId { get; set; }
public int ProvinceId { get; set; }
public List<SalonTreatments> MySalonTreatments;
}
public class SalonTreatments
{
public List<Treatment> SpecTypeTreaments { get; set; }
public TreatmentType TreatmentType { get; set; }
}
Controller :
public class SalonController : Controller
{
private CrsDatabase db = new CrsDatabase();
//
// GET: /Salon/Create
[Authorize]
[HttpGet]
public ActionResult Create()
{
return View(GetCreateSalonViewModel());
}
[Authorize]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(CreateSalonViewModel createSalonViewModel)
{
return View(createSalonViewModel);
}
#region service methods
private CreateSalonViewModel GetCreateSalonViewModel()
{
CreateSalonViewModel createSalonViewModel = new CreateSalonViewModel();
createSalonViewModel.MySalonTreatments = new List<SalonTreatments>();
foreach (var treatmentType in db.TreatmentType)
{
var treatmentOfSpecType = db.Treatment.Where(t => t.TreatmentType.Id == treatmentType.Id).ToList();
if (treatmentOfSpecType.Count > 0)
{
SalonTreatments salonTreatments = new SalonTreatments();
salonTreatments.TreatmentType = treatmentType;
salonTreatments.SpecTypeTreaments = treatmentOfSpecType;
createSalonViewModel.MySalonTreatments.Add(salonTreatments);
}
}
return createSalonViewModel;
}
#endregion
}
查看:
@model CRSWebsite.ViewModels.CreateSalonViewModel
@using (Html.BeginForm("Create", "Salon", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>CreateSalonViewModel</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EmailAddress, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EmailAddress)
@Html.ValidationMessageFor(model => model.EmailAddress)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Website, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Website)
@Html.ValidationMessageFor(model => model.Website)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.MobilePhone, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.MobilePhone)
@Html.ValidationMessageFor(model => model.MobilePhone)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LandlinePhone, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LandlinePhone)
@Html.ValidationMessageFor(model => model.LandlinePhone)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CityId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CityId)
@Html.ValidationMessageFor(model => model.CityId)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ProvinceId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProvinceId)
@Html.ValidationMessageFor(model => model.ProvinceId)
</div>
</div>
@for (int i = 0; i < Model.MySalonTreatments.Count; i++)
{
<label>Zabiegi @Model.MySalonTreatments[i].TreatmentType.Name:</label>
<div class="form-group">
<div class="col-md-10">
@for (int j = 0; j < Model.MySalonTreatments[i].SpecTypeTreaments.Count; j++)
{
@Html.CheckBoxFor(m => m.MySalonTreatments[i].SpecTypeTreaments[j].IsSelected)
@Html.DisplayFor(m => m.MySalonTreatments[i].SpecTypeTreaments[j].Name)
}
</div>
</div>
}
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
问候, 彼得
请您参考如下方法:
终于解决了我的问题!
这是一个微不足道的问题,在我的 View 模型类 CreateSalonViewModel 中,我将治疗集合创建为:
public List<SalonTreatments> MySalonTreatments;
但由于某些未知原因,模型 Binder 无法处理此问题。正确的做法是:
public List<SalonTreatments> MySalonTreatments { get; set; }
感谢您的帮助!
