Pio*_*cki 1 c# asp.net asp.net-mvc asp.net-mvc-4
正如我在标题中提到的,ViewModel 中的回发列表 (MySalonTreatments) 属性为空。
我是 MVC 的新手,但我认为它应该可以工作。模型绑定器知道如何处理复杂类型。我搜索了很多,但我没有得到任何解决方案。也许你可以指出我的错误。
视图模型:
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; }
}
Run Code Online (Sandbox Code Playgroud)
控制器:
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
}
Run Code Online (Sandbox Code Playgroud)
看法:
@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>
}
Run Code Online (Sandbox Code Playgroud)
问候, 彼得
终于我解决了我的问题!
这是一个小问题,在我的视图模型类 CreateSalonViewModel 中,我将治疗集合创建为:
public List<SalonTreatments> MySalonTreatments;
Run Code Online (Sandbox Code Playgroud)
但由于某些未知原因,模型绑定器无法处理此问题。正确的做法是:
public List<SalonTreatments> MySalonTreatments { get; set; }
Run Code Online (Sandbox Code Playgroud)
感谢帮助!