简体   繁体   English

ASP.NET MVC和Javascript / jQuery的设置?

[英]Setup for ASP.NET MVC and Javascript / jQuery?

My JavaScript will not run. 我的JavaScript无法运行。 I've been struggling with this for a while (2 days) Here's my current setup: 我一直在努力解决这个问题(2天)这是我目前的设置:

Created a new MVC 4 project. 创建了一个新的MVC 4项目。 I intend to try out some AJAX, so named it AjaxTest. 我打算尝试一些AJAX,所以将它命名为AjaxTest。 Added a model (Vehicle.cs) consisting of three classes OdometerInfo, VehicleMinInfo, and Vehicle. 添加了一个由OdometerInfo,VehicleMinInfo和Vehicle三个类组成的模型(Vehicle.cs)。 Here they are: 他们来了:

public class OdometerInfo
{
    public virtual string VIN { get; set;  }
    public virtual int Odometer { get; set; }
}

public class VehicleMinInfo : OdometerInfo
{
    public virtual Nullable<int> VehicleYear { get; set; }
    public virtual string Make { get; set; }
    public virtual string Model { get; set; }
}

public class Vehicle : VehicleMinInfo
{
    // default constructor
    public Vehicle()
    {
        VIN = "MyFakeVin";
        Odometer = 0;
        VehicleYear = 2012;
        Make = "Porsche";
        Model = "911";
    }

    public override string VIN { get; set; }
    public override int Odometer { get; set; }

    public override Nullable<int> VehicleYear { get; set; }
    public override string Make { get; set; }
    public override string Model { get; set; }

    // other fields
}

Then I replaced the contents of the template Index.cshtml with: 然后我将模板Index.cshtml的内容替换为:

@model AjaxTest.Models.VehicleMinInfo

@{
    ViewBag.Title = "Index";
}

<h2>Enter your odometer reading</h2>

@using (Html.BeginForm("Odometer", "Home", FormMethod.Post))
{
    <h4>For the following vehicle.</h4>
    @Html.DisplayFor(model => model.VIN) <br />
    @Html.DisplayFor(model => model.VehicleYear) <br />
    @Html.DisplayFor(model => model.Make) <br />
    @Html.DisplayFor(model => model.Model) <br />
    <h1>Enter Odometer</h1>
    @Html.DisplayNameFor(model => model.Odometer)
    @Html.EditorFor(model => model.Odometer)
    @Html.HiddenFor(model => model.VIN);
    <input type="submit" value="Odometer reading is correct" id="OdometerForm" />
}

Then I made a strongly typed view (Odometer.cshtml): 然后我做了一个强类型视图(Odometer.cshtml):

@model AjaxTest.Models.OdometerInfo
@{
    ViewBag.Title = "Odometer";
}

<h2>Odometer</h2>
Your odometer has been entered. It is
@Html.DisplayFor(model => model.Odometer)
. (
@Html.DisplayFor(model => model.VIN)
)

And added to the controller: 并添加到控制器:

    public ActionResult Index()
    {
        VehicleMinInfo OdomObj = new Vehicle();
        return View(OdomObj);
    }

    [HttpPost]
    public ActionResult Odometer(OdometerInfo oi)
    {
        return View(oi);
    }

All of that works. 所有这些都有效。 I can fill in an odometer reading and both the odometer and the VIN are passed back to the controller and displayed on the Odometer page. 我可以填写里程表读数,里程表和VIN都会传回控制器并显示在里程表页面上。 Now, it's time to start adding some JavaScript. 现在,是时候开始添加一些JavaScript了。 So I created OdometerList.js with the eventual goal of passing back a list of odometer readings instead of just one, and in it I placed: 所以我创建了OdometerList.js,最终的目标是传回一个里程表读数列表,而不仅仅是一个,在其中我放置:

$("#OdometerForm").click(function () {
    alert("Hello world!");
});

window.onload = function () {
    if (window.jQuery) {
        // jQuery is loaded  
        alert("Yeah!");
    } else {
        // jQuery is not loaded
        alert("Doesn't Work");
    }
}

$(document).ready(function () {
    alert("!!!");
});

Then I added in _Layout.cshtml 然后我在_Layout.cshtml中添加了

@Scripts.Render("~/Scripts/jquery-1.3.2.min.js")
@Scripts.Render("~/Scripts/OdometerList.js")

And I double checked Web.config to be sure compilation debug was true: 我仔细检查了Web.config以确保编译调试是真的:

<system.web>
    <compilation debug="true" targetFramework="4.5" />
    ...

None of my alerts are triggered, not one. 我的所有警报都没有被触发,而不是一个。 Is my setup wrong? 我的设置错了吗? I moved my JavaScript from OdometerList.js to the bottom of Odometer.cshtml and put it all between script tags, but there was no change. 我将我的JavaScript从OdometerList.js移到Odometer.cshtml的底部,并将它全部放在脚本标签之间,但没有任何变化。 I am new to JavaScript, so have I made a mistake there? 我是JavaScript的新手,所以我在那里犯了错误吗?

This is really stumping me. 这真的让我很难过。 Any help you can give will be much appreciated! 您将给予的任何帮助将不胜感激!

The version of jQuery had been updated. jQuery的版本已更新。 Once I put the correct version number in, I was able to use @Scripts.Render("~/Scripts/jquery-1.8.2.min.js") and everything works! 一旦我输入正确的版本号,我就可以使用@ Scripts.Render(“〜/ Scripts / jquery-1.8.2.min.js”),一切正常!

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

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