在局部视图中创建的下拉列表在渲染时没有在视图中正确验证

本文关键字:视图 验证 局部 创建 下拉列表 | 更新日期: 2023-09-27 18:04:29

我正在使用MVC创建一个项目。我有一个视图,其中用户将在文本框下拉列表中输入所有数据。

这些文本框和下拉列表创建在两个单独的部分视图,我渲染这些部分视图在一个视图

我的问题是textbxes得到验证正确,但下拉列表没有得到验证,即使当我选择值

当我只呈现一个显示文本框的部分视图时控件转到各自的动作方法。但当我渲染偏颇查看下拉列表;它会给我验证错误,即使我在下拉列表中选择值

我会张贴我的代码。

请记住,我只发布了代码片段,因为我的部分视图包含重复的文本框和下拉列表代码。

很抱歉这么长一段代码!!

显示文本框的部分视图代码
@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类,它包含属性具有需要呈现那些部分视图的数据。

下面是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在这里

这是我如何在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=""/>

谢谢. .

在局部视图中创建的下拉列表在渲染时没有在视图中正确验证

我已经复制了你的代码,我的工作正常,这就是我所做的

LookUpViewModel.cs

public class LookUpViewModel {
    [Display(Name = "Location")]
    [Required(ErrorMessage = "Please select a location")]
    public int SelectedLocation { get; set; }
    public SelectList LocationList { get; set; }
}

tblCurrentLocations.cs

public class tblCurrentLocations {
    public int LocationId { get; set; }
    public string Location {get;set;}
}

tblUser.cs

public class tblUser {
    [Display(Name = "First Name")]
    [Required(ErrorMessage = "First Name required")]
    public string FirstName {get; set;}
}

我在HomeControler上使用Index方法来渲染初始的View

public ActionResult Index() {
        var locations = new List<tblCurrentLocations>();
        locations.Add(new tblCurrentLocations {LocationId = 1, Location = "A"});
        locations.Add(new tblCurrentLocations {LocationId = 2, Location = "B"});
        var model = new WrapperViewModel();
        model.LookUpViewModel = new LookUpViewModel() {
            LocationList = new SelectList(locations, "LocationId", "Location")
        };
        return View(model);
    }

_CreateNewResource.cshtml

@model WebApplication1.Models.tblUser
<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.

_LookUpDropDowns.cshtml

model WebApplication1.Models.LookUpViewModel
@Html.LabelFor(m => m.SelectedLocation)
@Html.DropDownListFor(m => m.SelectedLocation, Model.LocationList, "-Please select-")
@Html.ValidationMessageFor(m => m.SelectedLocation)
最后是渲染所有Partials的视图
@model WebApplication1.Models.WrapperViewModel
@{
    ViewBag.Title = "Resources";
}
<script src="~/Scripts/jquery-2.1.4.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<h2>Resources</h2>
<div id="dialog">
@using (Html.BeginForm("AddResource", "Home", FormMethod.Post)) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    Html.RenderPartial("_CreateNewResource", new WebApplication1.Models.tblUser());
    Html.RenderPartial("_LookUpDropDowns", Model.LookUpViewModel);
    <br />
    <input type="submit" value="Create" />
}

关于validation和发布所选结果,这对我来说很好,你提到视图比你发布的更多,也许视图上的其他东西正在引起麻烦。还要注意,我必须添加<script src="~/Scripts/jquery-2.1.4.min.js"></script>才能使验证工作,您已经提交了