ASP.在ViewModel中包含List属性的.NET MVC Post Back List为空

本文关键字:List NET MVC Post 为空 Back 属性 ViewModel 包含 ASP | 更新日期: 2023-09-27 18:19:05

正如我在标题后提到的Post Back List (mysalontreating)属性在ViewModel是空的。

我是新的MVC,但我认为它应该工作。模型绑定器知道如何处理复杂类型。我搜索了很多,但我没有得到任何解决方案。也许你可以指出我的错误。

ViewModel:

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; }
}

控制器:

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>
}

问候,彼得。

ASP.在ViewModel中包含List属性的.NET MVC Post Back List为空

我终于解决了我的问题!

这是微不足道的问题,在我的视图模型类CreateSalonViewModel我创建治疗集合为:

public List<SalonTreatments> MySalonTreatments;

但是由于一些未知的原因,模型绑定器不能处理这个。正确的做法是:

public List<SalonTreatments> MySalonTreatments { get; set; }

谢谢你的帮助!

public ActionResult Create()似乎是用于显示表单的GET方法。您需要另一个方法来接受POST,并确保将HttpGet添加到第一个方法中,以确保MVC可以区分两者。还要验证表单是否使用POSt方法提交表单:

[Authorize][HttpGet]
public ActionResult Create()
{
    return View(GetCreateSalonViewModel());
}
[HttpPost]
public ActionResult Create(CreateSalonViewModel model) 
{  // MVC will try to bind your POSTed form to parameters
...
}