在部分视图中创建的下拉列表在呈现时未在视图中正确验证

时间:2015-07-24 06:48:57

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我正在使用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 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=""/>

谢谢..

1 个答案:

答案 0 :(得分:1)

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

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

我在Index上使用HomeControler方法呈现初始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>才能使验证工作,您已经无效