gpt4 book ai didi

c# - 在部分 View 中创建的下拉列表在呈现时未在 View 中正确验证

转载 作者:太空宇宙 更新时间:2023-11-03 13:03:31 29 4
gpt4 key购买 nike

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

那些文本框和下拉列表在两个单独的局部 View 中创建,我在一个 View 中呈现这些局部 View 。

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

When I render only one partial view which displays textboxes my control goes to respective Action Method. But when I render partial view for drop down lists; it gives me validation errors even when I select values in drop down lists

我会发布我的代码。

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

抱歉代码太长!!

显示文本框的局部 View 代码

@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>

显示下拉列表的局部 View 代码

@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

我在其中呈现这两个局部 View 的 View

@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 类,它包含属性,这些属性 具有呈现那些局部 View 所需的数据。

这是 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 操作方法

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

我使用了 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 的 View

@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 这对我来说效果很好并发布选定的结果,您提到 View 中的内容比您发布的内容多,也许 View 中的其他内容造成了麻烦。另请注意,我必须添加 <script src="~/Scripts/jquery-2.1.4.min.js"></script>让验证工作,你已经 ommited

关于c# - 在部分 View 中创建的下拉列表在呈现时未在 View 中正确验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31603981/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com