簡體   English   中英

在ViewModel中包含List屬性的Post Back List之后的ASP.NET MVC為空

[英]ASP.NET MVC After Post Back List that contains List property in ViewModel is null

正如我在ViewModel中的“發回列表”(MySalonTreatments)屬性之后的標題中所述,該屬性為null。

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

問候,彼得

終於,我解決了我的問題!

這是一個微不足道的問題,在我的viewmodel類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
...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM