CREATE FILE遇到操作系统错误5(访问被拒绝。)

时间:2016-08-25 20:28:23

标签: c# ajax custom-errors

我正在创建一个级联下拉列表,并在下拉列表初始化时收到错误 CREATE FILE遇到操作系统错误5(访问被拒绝。)。我构建了一个EF数据库第一个应用程序,不应该创建任何文件。不知道为什么会尝试创建任何文件。

我的级联下拉列表是制造商下拉列表,当选择另一个下拉列表时会加载相应的模型。

创建的ViewModel -

public class ManufacturerModelContext : DbContext
{

public DbSet<Manufacturer> Manufacturers { get; set; }
public DbSet<ManufacturerModel> ManufacturerModels { get; set; }
}

我用 -

填充下拉列表
//Populate the cascading dropdowns for manufacturer and model
ManufacturerModelContext mm = new ManufacturerModelContext();
[HttpGet]
public JsonResult GetManufacturers()
{

    var manufacturer = from a in mm.Manufacturers
                       select a.Manufacturer1;

    return Json(manufacturer.ToList(), JsonRequestBehavior.AllowGet);

}

public JsonResult GetModelsByManufacturerID(string manufacturerId)
{

    int Id = Convert.ToInt32(manufacturerId);

    var models = from a in mm.ManufacturerModels where a.ManufacturerID == Id select a;

    return Json(models);
}

我的ajax代码是 -

 $(function () {
    $.ajax({
        type: "GET",
        url: "GetManufacturers",
        datatype: "Json",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            $.each(data, function (index, value) {
                //alert("ManufacturerID: " + value.ManufacturerID + " Manufacturer: " + value.Manufacturer1);
                $('#dropdownManufacturer').append('<option value="' + value.ManufacturerID + '">' +
                value.Manufacturer1 + '</option>');
            });
        },
        error: function(error){
            alert("Error Ajax not working: " + error);
    }
    });

    $('#dropdownManufacturer').change(function () {
        $('#dropdownModel').empty();

        $.ajax({
            type: "POST",
            url: "GetModelsByManufacturerID",
            datatype: "Json",
            data: { manufacturerID: $('#dropdownManufacturer').val() },
            success: function (data) {

                $.each(data, function (index, value) {
                    $('#dropdownModel').append('<option value="' + value.ManufacturerID + '">' +
                        value.Model + '</option>');
                });
            },
            error: function (error) {
                alert("Error Ajax not working: " + error);
            }
        });
    });
});

我用 -

填充我的下拉列表
    <div class="form-group">
        @Html.LabelFor(model => model.ManufacturerModelID, "Manufacturer", new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("dropdownManufacturer", new SelectList(string.Empty, "Value", "Text"), "Please select a manufacturer", new { @style = "width:250;" })
            @Html.ValidationMessageFor(model => model.ManufacturerModelID)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.ManufacturerModelID, "Model", new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("dropdownModel", new SelectList(string.Empty, "Value", "Text"), "Please select a model", new { @style = "width:250;" })
            @Html.ValidationMessageFor(model => model.ManufacturerModel.Model)
        </div>
    </div>

更新 -

StackTrace错误

   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<NonQuery>b__0(DbCommand t, DbCommandInterceptionContext`1 c)
   at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 

1 个答案:

答案 0 :(得分:0)

也许你的sql server数据库没有运行,有权创建那个mdf数据库文件 您可以尝试运行您作为管理员使用的软件或检查该文件/目录的权限