2

In the head section of my _Layout.cshtml page I have this line of code...

<script src="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/Scripts/js")"></script>

I check the scripts directory and jquery-ui is in there just fine. I'm fairly new to MVC and especially MVC4. I've worked with 3 and I don't believe there was anything to do with Bundles from what I recall, or at least used. From what I get, this bundles up all the scripts into a tightly typed up text format taking out spaces and whatnot. So what I'm assuming is that jquery-ui is going to be added to each page since it's a shared page like a Master Page in Web Forms.

Now in my Index.cshtml file that uses this shared layout page I have at the top.

$(function () {
    $('#DateOfBirth').datepicker();
});

I've added a partial view with this code as well in my Index.cshtml file.

@Html.Partial("_SignUp", Model)

The partial view contains the field I'm trying to add it to. Unfortunately, it isn't adding the datepicker to the input field of type=text, and yes, the id="#DateOfBirth" for this field. What's the deal?

Edit: I do get this error - "Uncaught TypeError: Object [object Object] has no method 'datepicker'

4

2 回答 2

4

我看到的情况是,如果我将 javascipt 代码排除在外

<script type="text/javascript">
$(function () {
    $('#StartDateTime').datepicker();
});
</script> 

并将其放在 _Layout.cshtml 下方

@Scripts.Render("~/bundles/jquery","~/bundles/jqueryui")
@RenderSection("scripts", required: false) 

行(注意我必须包含 jqUi 包)它工作正常!!!

但我希望它以我希望它工作的方式工作。所以文档准备好在脚本完全加载之前被触发?

更新:只需用@section scripts {} 包围您的脚本,这将在页面底部的 jquery.js 文件下方呈现。

@section scripts {
<script type="text/javascript">
    $(function () {

        // Accordion
        $("#accordion").accordion({ header: "h3", active: 0, fillSpace: false });
        $("#bidTabs").tabs();
        $("#jobTabs").tabs();
        $("#settingsTabs").tabs(); 
    });
</script>
}
于 2012-06-28T12:29:52.353 回答
4

我看不出您的代码有任何问题,并且无法重现该问题(ASP.NET MVC 4 Beta)。以下对我来说很好:

  1. 使用 Internet 模板创建一个新的 ASP.NET MVC 4 项目
  2. 添加视图模型:

    public class MyViewModel
    {
        public DateTime DateOfBirth { get; set; }
    }
    
  3. 家庭控制器:

    public class HomeController : Controller
    {
         public ActionResult Index()
         {
            return View(new MyViewModel
            {
                DateOfBirth = DateTime.Now
            });
        }
    }
    
  4. Index.cshtml

    @model MyViewModel
    
    <script type="text/javascript">
    $(function () {
        $('#DateOfBirth').datepicker();
    });
    </script>
    
    @Html.Partial("_SignUp", Model)
    
  5. _SignUp.cshtml

    @model MyViewModel
    @Html.EditorFor(x => x.DateOfBirth)
    
  6. 结果:

在此处输入图像描述

所以我想现在问题变成了,你做了什么不同的事情?

于 2012-05-22T06:06:13.683 回答