【问题标题】:MVC Application with JQuery and CSS带有 JQuery 和 CSS 的 MVC 应用程序
【发布时间】:2016-05-25 13:18:08
【问题描述】:

我的带有 jQ​​uery 和 CSS 的 MVC 应用程序与 Localhost 一起工作,并按预期为我提供正确的输出,但是当我部署到 IIS 时,即使使用相同的代码,它也不会加载 jQuery 和 CSS .....任何帮助都会不胜感激......

HomeController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mvc_BrowserWindow.Models;

namespace Mvc_BrowserWindow.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Home/Details/5

        public ActionResult Details(int id)
        {
            SampleDbContext db = new SampleDbContext();
            Employee employee = db.Employees.Single(x => x.Id == id);
            return View(employee);
        }

        //
        // GET: /Home/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Home/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Home/Edit/5

        public ActionResult Edit(int id)
        {
            SampleDbContext db = new SampleDbContext();
            Employee employee=db.Employees.Single(x => x.Id == id);
            return View(employee);
        }

        //
        // POST: /Home/Edit/5

        [HttpPost]
        public ActionResult Edit(Employee employee)
        {
            if (ModelState.IsValid)
            {
                SampleDbContext db = new SampleDbContext();
                Employee employeeFromDb = db.Employees.Single(x => x.Id == employee.Id);
                employeeFromDb.FullName = employee.FullName;
                employeeFromDb.Gender = employee.Gender;
                employeeFromDb.Age = employee.Age;
                employeeFromDb.HireDate = employee.HireDate;
                employeeFromDb.Salary = employee.Salary;
                employeeFromDb.PersonalWebSite = employee.PersonalWebSite;

                db.ObjectStateManager.ChangeObjectState(employeeFromDb, System.Data.EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Details", new { id = employee.Id });
            }
            return View(employee);
        }

        //
        // GET: /Home/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Home/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}

Edit.csHtml

@model Mvc_BrowserWindow.Models.Employee

@{
    ViewBag.Title = "Edit";
}
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
<link href="../../Content/themes/base/jquery.ui.all.css" rel="stylesheet" type="text/css" />
<script src="../../Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-ui-1.8.20.min.js" type="text/javascript"></script>

<script type="text/javascript">
     $(function () {
         $("input:text.date").datepicker(
        {
            dateFormat: "dd/mm/yy"
        });
     });
</script>

@using (@Html.BeginForm())
{    
    @Html.EditorForModel()
    <br />
    <input type="submit" value="Save" />
}

DateTime.csHtml:

@model DateTime?
@Html.TextBox("", (Model.HasValue ? Model.Value.ToString("dd/MM/yyyy") : string.Empty), new { @class = "date" })

【问题讨论】:

    标签: asp.net-mvc iis model-view-controller


    【解决方案1】:

    您的问题可能是由于您的链接和脚本包含使用了相对路径。您应该改用绝对路径,例如(假设 ContentScripts 文件夹位于网站的根文件夹中)。

    <link href="/Content/Site.css" rel="stylesheet" type="text/css" />
    <link href="/Content/themes/base/jquery.ui.all.css" rel="stylesheet" type="text/css" />
    <script src="/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script src="/Scripts/jquery-ui-1.8.20.min.js" type="text/javascript"></script>
    

    .. 表示相对于当前路径,因此如果您的 MVC URL 的 / 数量不同,那么相对路径将不起作用。以/ 开头的路径使它们成为网站根目录的绝对路径。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-05
      • 2023-03-12
      • 1970-01-01
      • 2011-10-07
      • 2013-06-21
      • 1970-01-01
      • 2010-10-30
      相关资源
      最近更新 更多