简体   繁体   English

在MVC3中将jQuery添加到具有相同模型的表单

[英]Adding jQuery to forms with the same model in MVC3

I am trying to have two submission forms with similar functionality on the same view, where each form is strongly typed to the same model. 我正在尝试在同一视图上使用具有相似功能的两个提交表单,其中每种表单都必须严格键入同一模型。 I then am trying to add a datepicker to the same input on both forms, but I am unsure of how to do this. 然后,我试图将日期选择器添加到两种形式的相同输入中,但是我不确定如何执行此操作。 Here is my code: 这是我的代码:

@using (@Ajax.BeginForm(...
                        new { id = "editScheduleForm" }))
{
    <div class="editor-label">
        @Html.LabelFor(model => model.StartTime)
    </div>
    <div class="editor-field">
        <input 
        @Html.EditorFor(model => model.StartTime)
        @Html.ValidationMessageFor(model => model.StartTime)
    </div>
}

... ...

@using (@Ajax.BeginForm(...
                        new { id = "addScheduleForm" }))
{
    <div class="editor-label">
        @Html.LabelFor(model => model.StartTime)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.StartTime)
        @Html.ValidationMessageFor(model => model.StartTime)
    </div>
}

Both of these forms are in their own strongly-typed partial views. 这两种形式都具有自己的强类型局部视图。 I naturally tried simply adding the datepicker in jQuery to both partial views, like so: 我自然尝试将jQuery中的datepicker添加到两个局部视图中,如下所示:

$(function () {
    $("#StartTime").datepicker();
});

But it unsurprisingly only worked for one of the inputs. 但是毫不奇怪,它仅对输入之一起作用。 I have been trying to use the HTML id that I added to both Ajax form declarations (eg editScheduleForm and addScheduleForm), but am unsure of how to do this. 我一直在尝试使用添加到两个Ajax表单声明中的HTML ID(例如,editScheduleForm和addScheduleForm),但是不确定如何执行此操作。 Any suggestions? 有什么建议么?

Solution: As scottm suggested, I looked through documentation for using a custom editor template. 解决方案:正如scottm所建议的,我浏览了有关使用自定义编辑器模板的文档。 I read this, detailing functionality I didn't know existed: http://msdn.microsoft.com/en-us/library/ee407399.aspx 我读了这篇,详细介绍了我不知道的功能: http : //msdn.microsoft.com/zh-cn/library/ee407399.aspx

Here, you can specify a template, and then add a parameter for the htmlFieldName, which is specifically designed to circumvent the problem I was happening. 在这里,您可以指定一个模板,然后为htmlFieldName添加一个参数,该参数专门用于规避我正在发生的问题。

You can add a class to the inputs in your editor template. 您可以在编辑器模板的输入中添加一个类。 Then you can then use the class as the jQuery selector. 然后,您可以将该类用作jQuery选择器。

Add a file called DateTime.cshtml under ~/Views/Shared/EditorTemplates , and add the following: ~/Views/Shared/EditorTemplates下添加一个名为DateTime.cshtml的文件,并添加以下内容:

@model DateTime
@Html.TextBox("", (Model.HasValue ? Model.Value.ToShortDateString() : string.Empty), new { @class = "date" })

Then add this jQuery to the page. 然后将此jQuery添加到页面。

$(function () {
    $(".date").datepicker();
});

The ID must be unique in a document. 该ID在文档中必须唯一。

Never rely on multiple ids on a page working correctly. 切勿依赖页面上的多个ID正常工作。 Matter of fact, despite what most examples in blogs do, you should hardly ever use ids as jquery selectors. 实际上,尽管博客中的大多数示例都可以做到,但您几乎不应该将id用作jquery选择器。

Like @scottm said. 就像@scottm说的那样。 You should use classes to tag items which you want to be widget-ized. 您应该使用类标记要进行窗口小部件化的项目。

Here's a simplistic bit of code so you can create jq ui widgets declaratively: 这是一些简单的代码,因此您可以声明性地创建jq ui小部件:

$(function() {

  $('[data-usewidget]').each(function() {
    var $this = $(this), widget = $this.data('usewidget');
    $.fn[widget] && $this[widget]();
  });

});

Then in your html 然后在你的HTML

<input name=startTime data-usewidget=datepicker />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM