使用复杂模型验证局部视图?
我有一个时间表应用程序,我在其中使用 jQuery 加载 PartialView。一切正常,但我不知道如何验证输入。我尝试按照以前的方式进行操作,但它不起作用:
<% for (int i = 0; i < Model.Tasks.Count; i++)
{
var task = Model.Tasks[i];
%>
<tr class="taskrow">
<td class="customer">
<%: task.Customer.CustomerName %>
</td>
<td class="task">
<%: task.TaskName %>
</td>
<% for (int j = 0; j < task.TimeSegmentList.Count; j++)
{ %>
<td>
<%: Html.TextBoxFor(model => model.Tasks[i].TimeSegmentList[j].Hours, new { @class = "hourInput" })%>
<%: Html.ValidationMessageFor(model => model.Tasks[i].TimeSegmentList[j].Hours)%>
</td>
<% } %>
</tr>
<% } %>
虽然此 PartialView 是使用 jQuery 加载的,但它还包含一个提交到操作方法的提交按钮。
因为我使用的是实体框架,所以我读到我需要使用 DataAnnotations 来装饰模型,如下所示:
[MetadataType(typeof(TimeSegmentMetaData))]
public partial class TimeSegment
{
public class TimeSegmentMetaData
{
[Range(0,24,ErrorMessage = "Must be between 0 and 24 hours")]
public object Hours { get; set; }
}
}
所以我想确保在此处输入的 Hours 是 0 到 24 之间的数字。 (我还想确保它是一个 int,但我还没有做到这一点,需要首先进行验证)
所以这不起作用,我做错了什么?是否无法执行此操作,因为我在 PartialView 中有带有 for 循环的复杂模型?
我也在考虑尝试找到某种严格的 jQuery 验证(我已经看到为此存在 jQuery 插件),但我不知道如何在 PartialView 中使用 jQuery。因为当 document.ready 函数位于主(父)视图中时,如何让 jQuery 运行?无论如何我都需要这个,因为以后我希望能够在用户选择输入字段时执行一些操作...
PS:我对 jQuery 和 MVC 仍然相当陌生,所以我真的很感激关于以下方面的明确指示:我做错了什么...谢谢!
I have a timesheet application where I load a PartialView with jQuery. Everything works fine, but I have no idea how to validate the input. I have tried to do it similarly to how I've done it before, but it doesn't work:
<% for (int i = 0; i < Model.Tasks.Count; i++)
{
var task = Model.Tasks[i];
%>
<tr class="taskrow">
<td class="customer">
<%: task.Customer.CustomerName %>
</td>
<td class="task">
<%: task.TaskName %>
</td>
<% for (int j = 0; j < task.TimeSegmentList.Count; j++)
{ %>
<td>
<%: Html.TextBoxFor(model => model.Tasks[i].TimeSegmentList[j].Hours, new { @class = "hourInput" })%>
<%: Html.ValidationMessageFor(model => model.Tasks[i].TimeSegmentList[j].Hours)%>
</td>
<% } %>
</tr>
<% } %>
Although this PartialView is loaded using jQuery, it also contains a submit button which submits to an action method.
Because I'm using the Entity Framework I have read that I need to decorate the model with DataAnnotations like this:
[MetadataType(typeof(TimeSegmentMetaData))]
public partial class TimeSegment
{
public class TimeSegmentMetaData
{
[Range(0,24,ErrorMessage = "Must be between 0 and 24 hours")]
public object Hours { get; set; }
}
}
So I want to make sure it's a number between 0 and 24 entered here for Hours. (I would also like to make sure it's an int, but I haven't gotten to that yet, need to make validation work at all first)
So this doesn't work, what am I doing wrong? Is it not possible to do this because I have the complex model with the for loop in the PartialView?
I was also thinking of trying to find some sort of strict jQuery validation (I've seen jQuery plugins exist for this), but I can't figure out how to use jQuery within the PartialView. Because how can I get jQuery to run when the document.ready function is within the main (parent) view? I would need this anyway, because later I want to be able to do things if a user selects an input field...
PS: I'm still rather new to both jQuery and MVC, so I'd really appreciate clear pointers as to what I'm doing wrong... Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我首先将类型更改为 Int。另外,您可以使用 foreach 而不是 For 循环
I would start by changing the type to an Int. Also, you can use foreach instead of the For loop