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

C# 在局部视图中创建的下拉列表在渲染时未在视图中正确验证,c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我正在使用MVC创建一个项目。我有一个视图,在该视图中,用户将在文本框和下拉列表中输入所有数据 这些文本框和下拉列表是在两个独立的局部视图中创建的,我在一个视图中呈现这些局部视图 我的问题是textbx是否正确验证但下拉列表即使在我选择值时也无法验证 当我只渲染一个显示文本框的局部视图时 控件转到相应的操作方法。但当我渲染部分 查看下拉列表;即使在我 在下拉列表中选择值 @model PITCRoster.ViewModel.LookUpViewModel <script src="~/C

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

这些文本框和下拉列表是在两个独立的局部视图中创建的,我在一个视图中呈现这些局部视图

我的问题是textbx是否正确验证下拉列表即使在我选择值时也无法验证

当我只渲染一个显示文本框的局部视图时 控件转到相应的操作方法。但当我渲染部分 查看下拉列表;即使在我 在下拉列表中选择值

@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.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>
要理解这门课,请参考我的问题和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的操作方法

  [HttpPost]
        public ActionResult AddResource(LookUpViewModel modelLookUp, tblUser tblUser)
        {
            Helpers.CopyLookUpViewModelTotblUser(modelLookUp, tblUser);
            return View(modelLookUp);
        }
编辑

为DropDownListFor()生成的HTML:


它还有

为ValidationMessageFor()生成的HTML:



谢谢你。

我复制了你的代码,我的代码很好,这就是我所做的

LookUpViewModel.cs

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

    public SelectList LocationList { get; set; }
}
public class tblCurrentLocations {
    public int LocationId { get; set; }
    public string Location {get;set;}
}
public class tblUser {
    [Display(Name = "First Name")]
    [Required(ErrorMessage = "First Name required")]
    public string FirstName {get; set;}
}
tblCurrentLocations.cs

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

    public SelectList LocationList { get; set; }
}
public class tblCurrentLocations {
    public int LocationId { get; set; }
    public string Location {get;set;}
}
public class tblUser {
    [Display(Name = "First Name")]
    [Required(ErrorMessage = "First Name required")]
    public string FirstName {get; set;}
}
tblUser.cs

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

    public SelectList LocationList { get; set; }
}
public class tblCurrentLocations {
    public int LocationId { get; set; }
    public string Location {get;set;}
}
public class tblUser {
    [Display(Name = "First Name")]
    [Required(ErrorMessage = "First Name required")]
    public string FirstName {get; set;}
}
我在我的
HomeControler
上使用了
Index
方法来呈现初始的
视图

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);
    }

\u LookUpDropDowns.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.
model WebApplication1.Models.LookUpViewModel

@Html.LabelFor(m => m.SelectedLocation)

@Html.DropDownListFor(m => m.SelectedLocation, Model.LocationList, "-Please select-")

@Html.ValidationMessageFor(m => m.SelectedLocation)
最后是呈现所有
部分的视图

@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" />
}
@model WebApplication1.Models.WrapperViewModel
@{
ViewBag.Title=“资源”;
}
资源
@使用(Html.BeginForm(“AddResource”、“Home”、FormMethod.Post)){
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
Html.RenderPartial(“_CreateNewResource”,newWebApplication1.Models.tblUser());
RenderPartial(“_LookUpDropDowns”,Model.LookUpViewModel);

}


关于
验证
和发布所选结果,这对我来说效果很好,您提到视图中的内容比您发布的内容更多,可能视图中的其他内容造成了麻烦。另请注意,我必须添加
,以使验证工作正常,您建议了您可以在POST方法中检查
ModelState
,并让我们知道错误是什么吗are@StephenMuecke当我在输入所有值后单击create按钮时,它仍然停留在页面上,只要求在下拉列表中选择值。所以我想我不能检查modelstate,你也有问题,因为你在复制脚本。切勿将脚本放在局部视图中-仅在主视图中包含它们为了证明这一点,请使用
var locations=new[]{new{LocationId=1,Location=“Location 1”},new{LocationId=2,Location=“Location 2”});model.LocationList=new SelectList(locations,“LocationId”,“Location”);
@3dd,必须这样做,否则会引发异常。请参阅我对问题的最后一条评论,以了解真正的问题是什么:)检查是否使用正确的值填充
SelectList