我有一个表单使用POST
方法将数据发送到我的控制器上的Create
操作(注释为[HttpPost]
)。动作签名需要Visit
。问题是只有Visit
上的一些属性被填充,尽管视图中的所有属性都填充在屏幕上。仅填充将数据输入到文本框中的属性。下拉列表中没有任何值传递给模型。
有什么想法在这里发生了什么?谢谢!
MVC 4 Beta。
更新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>
}
答案 0 :(得分: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();
答案 1 :(得分: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来保存这些列表的信息。
答案 2 :(得分:0)
使用一个属性映射到一个属性以及一个包含可用选项的属性...例如PsychometristId和心理医生列表。然后使用DropDownListFor而不是DropDownList。 试试这样的事情
@Html.DropDownListFor("PsychometristId", new SelectList(Psychometrists, "name", "id"))