【发布时间】:2015-07-24 06:48:56
【问题描述】:
我正在使用 MVC 创建项目。我有一个视图,用户可以在其中输入文本框和下拉列表中的所有数据。
那些文本框和下拉列表是在两个单独的局部视图中创建的,我将在一个视图中呈现这些局部视图。
我的问题是 textbxes 正在得到正确验证,但 即使我选择了值,下拉列表也没有得到验证。
当我只渲染一个显示文本框的局部视图时 控制转到相应的操作方法。但是当我渲染部分 查看下拉列表;它给了我验证错误,即使我 在下拉列表中选择值
我将发布我的代码。
请记住,我只发布了 sn-p 的代码,因为我的部分视图包含文本框和下拉列表的重复代码。
抱歉,sn-ps 的代码太长了!!
显示文本框的局部视图代码
@model PITCRoster.tblUser
<script src="~/Content/CustomScripts/DatePickers.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<fieldset>
<legend>tblUser</legend>
<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
// many other textboxes.
</fieldset>
用于显示下拉列表的局部视图的代码
@model PITCRoster.ViewModel.LookUpViewModel
<script src="~/Content/CustomScripts/DatePickers.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@Html.LabelFor(m => m.SelectedLocation)
@Html.DropDownListFor(m => m.SelectedLocation, Model.LocationList, "-Please select-")
@Html.ValidationMessageFor(m => m.SelectedLocation)
//many other dropdownlists
我在其中渲染这两个部分视图的视图
@model PITCRoster.ViewModel.WrapperViewModel
@{
ViewBag.Title = "Resources";
}
<script src="~/Content/PopUp.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<h2>Resources</h2>
@{
//Html.RenderPartial("_DisplayResources")
Html.RenderPartial("_DisplayResources",Model.tblUser);
}
<div id="dialog">
@using (Html.BeginForm("AddResource", "Resources", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
Html.RenderPartial("_CreateNewResource", new PITCRoster.tblUser());
Html.RenderPartial("_LookUpDropDowns", Model.LookUpViewModel);
<br />
<input type="submit" value="Create" />
}
</div>
这里的 WrapperViewModel 是一个 ViewModel 类,其中包含 properties 具有 呈现这些部分视图所需的数据。
这是 WrapperViewModel 的代码
public class WrapperViewModel
{
//tblUser will be a property of class tblUser.
public IEnumerable<tblUser> tblUser { get; set; }
//It will contain property of class LookUpViewModel.
public LookUpViewModel LookUpViewModel { get; set; }
}
所有文本框都来自tblUser类 所有 下拉列表 列表都来自 LookUpViewModel
类这是我的 LookUpViewModel 类
public class LookUpViewModel
{
[Display(Name = "Location")]
[Required(ErrorMessage = "Please select a location")]
public int SelectedLocation { get; set; }
public SelectList LocationList { get; set; }
}
要了解这门课,请参考我的问题和解决方案 Stephen Muecke here
这是我在 LocationList
中填充数据的方式 RosterManagementEntities rosterManagementContext = new RosterManagementEntities();
// populate your select lists
var locations = from o in rosterManagementContext.tblCurrentLocations select o;
model.LocationList = new SelectList(locations, "LocationId", "Location");
这是我的 AddResource 的 Action 方法
[HttpPost]
public ActionResult AddResource(LookUpViewModel modelLookUp, tblUser tblUser)
{
Helpers.CopyLookUpViewModelTotblUser(modelLookUp, tblUser);
return View(modelLookUp);
}
编辑
为 DropDownListFor() 生成的 HTML:
<select name="SelectedLocation" id="SelectedLocation" data-val-required="Please select a location" data-val="true" data-val-number="The field Location must be a number." value="">
它还有<option value = "">
为 ValidationMessageFor() 生成的 HTML:
<span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="SelectedLocation" value=""/>
谢谢你..
【问题讨论】:
-
您能否检查 POST 方法中的
ModelState并告诉我们错误是什么 -
@StephenMuecke 当我在输入所有值后单击创建按钮时,它仍然卡在页面上,只要求在下拉列表中选择值。所以我想我无法检查 ModelState
-
您也有问题,因为您正在复制脚本。切勿将脚本放在部分视图中 - 仅将它们包含在主视图中
-
为了证明这一点,请使用
var locations = new[] { new { LocationId = 1, Location = "Location 1" }, new { LocationId = 2, Location = "Location 2" }); model.LocationList = new SelectList(locations, "LocationId", "Location"); -
@3dd,必须,否则会抛出异常。
标签: c# asp.net asp.net-mvc asp.net-mvc-4