0

我有一个表单,它使用该POST方法将数据发送到我的控制器上的Create操作(用 注释)。[HttpPost]动作签名采用Visit. 问题是只有一些属性Visit被填充,尽管所有属性都被填充在视图的屏幕上。仅填充将数据输入到文本框中的属性。下拉列表中的任何值都没有传递给模型。

有什么想法吗?谢谢!

MVC 4 测试版。

更新 1-视图中使用的表单...

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Visit</legend>
            <div class="editor-label">
                @Html.LabelFor(model => model.Visit.VisitDate)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Visit.VisitDate, new { @class = "editor-field-medium" })
                @Html.ValidationMessageFor(model => model.Visit.VisitDate)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.Visit.NPN)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Visit.NPN, new { @class = "editor-field-medium" })
                @Html.ValidationMessageFor(model => model.Visit.NPN)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.Visit.AppointmentTypeId)
            </div>
            <div class="editor-field"> @* THIS DOES NOT GET POPULATED WHEN PASSED TO THE ACTION*@
                @Html.DropDownList("AppointmentTypeId", String.Empty)
                @Html.ValidationMessageFor(model => model.Visit.AppointmentTypeId)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.Visit.EducationId)
            </div>
            <div class="editor-field"> @* THIS DOES NOT GET POPULATED WHEN PASSED TO THE ACTION*@
                @Html.DropDownList("EducationId", String.Empty)
                @Html.ValidationMessageFor(model => model.Visit.EducationId)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.Visit.PsychologistId)
            </div>
            <div class="editor-field"> @* THIS DOES NOT GET POPULATED WHEN PASSED TO THE ACTION*@
                @Html.DropDownList("PsychologistId", String.Empty)
                @Html.ValidationMessageFor(model => model.Visit.PsychologistId)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.Visit.PsychometristId)
            </div>
            <div class="editor-field"> @* THIS DOES NOT GET POPULATED WHEN PASSED TO THE ACTION*@
                @Html.DropDownList("PsychometristId", String.Empty)
                @Html.ValidationMessageFor(model => model.Visit.PsychometristId)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.Visit.PostDoctoralId)
            </div>
            <div class="editor-field"> @* THIS DOES NOT GET POPULATED WHEN PASSED TO THE ACTION*@
                @Html.DropDownList("PostDoctoralId", String.Empty)
                @Html.ValidationMessageFor(model => model.Visit.PostDoctoralId)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.Visit.BehavioralObservations)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Visit.BehavioralObservations)
                @Html.ValidationMessageFor(model => model.Visit.BehavioralObservations)
            </div>
        <p>
            <input type="submit" value="Create Visit" />
        </p>
    </fieldset>
}
4

3 回答 3

1

您的模型状态可能无效。这是一个扩展方法,它将读取模型状态并将错误转储到调试日志:

public static class ModelExtensions
{
    public static void DumpErrors(this System.Web.Mvc.ModelStateDictionary ModelState)
    {
        var errors = from key in ModelState
                     let errorList = ModelState[key.Key].Errors
                     where errorList.Any()
                     select new
                     {
                         Item = key.Key,
                         Value = key.Value,
                         errorList
                     };

        foreach (var errorList in errors)
        {
            System.Diagnostics.Debug.WriteLine("MODEL ERROR:");
            System.Diagnostics.Debug.WriteLine(errorList.Item);
            System.Diagnostics.Debug.WriteLine(errorList.Value);
            foreach (var error in errorList.errorList)
            {
                System.Diagnostics.Debug.WriteLine(error.ErrorMessage);
                System.Diagnostics.Debug.WriteLine(error.Exception);
            }
            System.Diagnostics.Debug.WriteLine("-----");
        }
    }
}

在有问题的操作的第一行调用它,在其中放置一个断点,然后查看它的内容。

ModelState.DumpErrors();
于 2012-05-16T21:56:17.340 回答
0

使用一个属性映射到一个属性,一个包含可用选项...例如 PsychometristId 和 Psychometrists 列表。然后使用 DropDownListFor 代替 DropDownList。尝试这样的事情

 @Html.DropDownListFor("PsychometristId", new SelectList(Psychometrists, "name", "id"))
于 2012-05-17T20:23:16.217 回答
0

您的下拉列表没有填充任何内容。你有:

@Html.DropDownList("AppointmentTypeId", String.Empty)

因此,您没有可供选择的选项。AppointmentTypeId你的财产Visit永远是空的。您的选择列表应填充具有相关文本和值的选项:

看法

@{
    List<Appointment> appointmentTypes = ViewBag.AppointmentTypeOptions;
    var appointmentList = new SelectList(list, "AppointmentID", "Description");
}
@Html.DropDownListFor(x => x.AppointmentTypeId, appointmentList)

控制器

ViewBag.AppointmentTypeOptions = someDataStore.getAppointmentTypes();

请注意,我曾经ViewBag使这变得更简单,并允许在 HTTP 帖子中继续使用您的 Visit 类。如果您愿意,可以创建一个 ViewModel 来保存这些列表的信息。

于 2012-05-17T14:27:58.693 回答