AngularJS在编辑模式下填充DropDown

时间:2016-07-13 18:07:38

标签: c# angularjs drop-down-menu edit

以下是练习代码中提到的问题。

C#代码:

public JsonResult fillLocationsList()
{
    var list = db.DBset_ScadaLocations.Select(x => new { x.LocID, x.Location }).ToList();
    return Json(list,JsonRequestBehavior.AllowGet);

AngularJS控制器功能:

ScadaAMRlog_Factory.fillReasonsList().success(function (data) {
   $scope.ScadaLocations = data;
});

HTML代码:

<select class="form-control" ng-model="ScadaAMRlog.LocationID" ng-options="b.LocID as b.Location for b in ScadaLocations">
    <option value="">-- Select a Book --</option>
</select>

此代码不会填充从DropDownList到数据库后获取的ScadaAMRlog.LocatoinID。 请根据DropDownList帮助在编辑模式下填充ScadaAMRlog.LocatoinID所选下拉列表。

这是我的数据访问层代码:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace DAL.Models
{
    public class DbManager: IDisposable
    {
        private bool _disposed;
        private SqlConnection cn;
        private string connectionString;
        private string parameters;
        private string procName;

        public DbManager(string dbConnectionString)
        {
            this.connectionString = dbConnectionString;
        }

        public DbManager(string dbConnectionString, string storedProcedureName)
        {
            this.procName = storedProcedureName;
            this.connectionString = dbConnectionString;
        }

        public void AddParameter(string name, object data)
        {
            this.parameters = string.Concat(new object[] { this.parameters, name, "=", data });
        }

        public void Close()
        {
            if (this.cn != null)
            {
                this.cn.Close();
            }
        }

        private SqlCommand CreateCommand(string procName, SqlParameter[] prams)
        {
            this.Open();
            SqlCommand command = new SqlCommand(procName, this.cn);
            if (command.Parameters.Count > 0)
            {
                command.Parameters.Clear();
            }
            command.CommandType = CommandType.StoredProcedure;
            if (prams != null)
            {
                foreach (SqlParameter parameter in prams)
                {
                    command.Parameters.Add(parameter);
                }
            }
            command.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null));
            return command;
        }

        public virtual void Dispose()
        {
            this.Dispose(false);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!this.Disposed)
            {
                try
                {
                    GC.SuppressFinalize(this);
                    if (this.cn != null)
                    {
                        this.cn.Dispose();
                    }
                    this.cn = null;
                }
                catch
                {
                }
                finally
                {
                    this.Disposed = true;
                }
            }
        }

        public int ExecuteScalar()
        {
            int num2;
            try
            {
                if (this.procName == "")
                {
                    throw new Exception("Procedure name must be set.");
                }
                this.Open();
                SqlCommand command = new SqlCommand(this.procName + " " + this.parameters, this.cn);
                int num = (int)command.ExecuteScalar();
                num2 = num;
            }
            finally
            {
                this.Close();
            }
            return num2;
        }

        public DataSet FillDataSet(DataSet dataset)
        {
            DataSet set;
            try
            {
                this.Open();
                new SqlDataAdapter(this.procName + " " + this.parameters, this.cn).Fill(dataset);
                set = dataset;
            }
            finally
            {
                this.Close();
            }
            return set;
        }

        ~DbManager()
        {
            this.Dispose(true);
        }

        public SqlDataReader GetDataReader()
        {
            if (this.procName == "")
            {
                throw new Exception("Procedure name must be set.");
            }
            this.Open();
            SqlCommand command = new SqlCommand(this.procName + " " + this.parameters, this.cn);
            return command.ExecuteReader(CommandBehavior.CloseConnection);
        }

        public SqlDataReader GetDataReader(string procName)
        {
            return this.CreateCommand(procName, null).ExecuteReader(CommandBehavior.CloseConnection);
        }

        public SqlDataReader GetDataReader(string procName, params SqlParameter[] prams)
        {
            return this.CreateCommand(procName, prams).ExecuteReader(CommandBehavior.CloseConnection);
        }

        public SqlDataReader GetDataReader(string procName, CommandType type, params SqlParameter[] prams)
        {
            SqlCommand command = this.CreateCommand(procName, prams);
            command.CommandType = type;
            return command.ExecuteReader(CommandBehavior.CloseConnection);
        }

        public SqlDataReader GetDataReader(string procName, CommandType type, CommandBehavior cmdBehavior, params SqlParameter[] prams)
        {
            SqlCommand command = this.CreateCommand(procName, prams);
            command.CommandType = type;
            return command.ExecuteReader(cmdBehavior);
        }

        public DataSet GetDataSet()
        {
            DataSet set2;
            try
            {
                if (this.procName == "")
                {
                    throw new Exception("Procedure name must be set.");
                }
                this.Open();
                SqlDataAdapter adapter = new SqlDataAdapter(this.procName + " " + this.parameters, this.cn);
                DataSet dataSet = new DataSet();
                adapter.Fill(dataSet);
                set2 = dataSet;
            }
            finally
            {
                this.Close();
            }
            return set2;
        }

        public DataSet GetDataSet(string name)
        {
            DataSet set2;
            try
            {
                this.Open();
                SqlDataAdapter adapter = new SqlDataAdapter(this.procName + " " + this.parameters, this.cn);
                DataSet dataSet = new DataSet(name);
                adapter.Fill(dataSet);
                set2 = dataSet;
            }
            finally
            {
                this.Close();
            }
            return set2;
        }

        public DataSet GetDataSet(string procName, SqlParameter[] prams)
        {
            DataSet set2;
            try
            {
                SqlDataAdapter adapter = new SqlDataAdapter(this.CreateCommand(procName, prams));
                DataSet dataSet = new DataSet();
                adapter.Fill(dataSet);
                set2 = dataSet;
            }
            finally
            {
                this.Close();
            }
            return set2;
        }

        public DataSet GetDataSet(string name, int startRow, int maxRecords)
        {
            DataSet set2;
            try
            {
                if (this.procName == "")
                {
                    throw new Exception("Procedure name must be set.");
                }
                this.Open();
                SqlDataAdapter adapter = new SqlDataAdapter(this.procName + " " + this.parameters, this.cn);
                DataSet dataSet = new DataSet(name);
                adapter.Fill(dataSet, startRow, maxRecords, name);
                set2 = dataSet;
            }
            finally
            {
                this.Close();
            }
            return set2;
        }

        public DataView GetDataView(string name)
        {
            DataView defaultView;
            try
            {
                if (this.procName == "")
                {
                    throw new Exception("Procedure name must be set.");
                }
                this.Open();
                SqlDataAdapter adapter = new SqlDataAdapter(this.procName + " " + this.parameters, this.cn);
                DataSet dataSet = new DataSet(name);
                adapter.Fill(dataSet, name);
                defaultView = dataSet.Tables[0].DefaultView;
            }
            finally
            {
                this.Close();
            }
            return defaultView;
        }

        public DataView GetDataView(string name, int startRow, int maxRecords)
        {
            DataView defaultView;
            try
            {
                if (this.procName == "")
                {
                    throw new Exception("Procedure name must be set.");
                }
                this.Open();
                SqlDataAdapter adapter = new SqlDataAdapter(this.procName + " " + this.parameters, this.cn);
                DataSet dataSet = new DataSet(name);
                adapter.Fill(dataSet, startRow, maxRecords, name);
                defaultView = dataSet.Tables[0].DefaultView;
            }
            finally
            {
                this.Close();
            }
            return defaultView;
        }

        public static DbManager GetDbManager()
        {
            return new DbManager(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        }

        public static DbManager GetDbManager(string connectionStringName)
        {
            return new DbManager(ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString);
        }

        public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value)
        {
            return this.MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
        }

        public SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size)
        {
            return this.MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null);
        }

        public SqlParameter MakeParam(string ParamName, SqlDbType DbType, int Size, ParameterDirection Direction, object Value)
        {
            SqlParameter parameter;
            if (Size > 0)
            {
                parameter = new SqlParameter(ParamName, DbType, Size);
            }
            else
            {
                parameter = new SqlParameter(ParamName, DbType);
            }
            parameter.Direction = Direction;
            if ((Direction != ParameterDirection.Output) || (Value != null))
            {
                parameter.Value = Value;
            }
            return parameter;
        }

        public SqlParameter MakeReturnParam(SqlDbType DbType, int Size)
        {
            return this.MakeParam("@return_value", DbType, Size, ParameterDirection.ReturnValue, null);
        }

        private void Open()
        {
            if (this.cn == null)
            {
                this.cn = new SqlConnection(this.connectionString);
                this.cn.Open();
            }
        }

        public int RunProc(string procName)
        {
            int num;
            try
            {
                SqlCommand command = this.CreateCommand(procName, null);
                command.ExecuteNonQuery();
                num = (int)command.Parameters["ReturnValue"].Value;
            }
            finally
            {
                this.Close();
            }
            return num;
        }

        public void RunProc(string procName, out SqlDataReader dataReader)
        {
            dataReader = this.CreateCommand(procName, null).ExecuteReader(CommandBehavior.CloseConnection);
        }

        public int RunProc(string procName, SqlParameter[] prams)
        {
            int num;
            try
            {
                SqlCommand command = this.CreateCommand(procName, prams);
                command.ExecuteNonQuery();
                SqlParameter parameter = command.Parameters["ReturnValue"];
                if ((parameter != null) && (parameter.Value != null))
                {
                    return (int)parameter.Value;
                }
                num = 0;
            }
            finally
            {
                this.Close();
            }
            return num;
        }

        public void RunProc(string procName, SqlParameter[] prams, out SqlDataReader dataReader)
        {
            dataReader = this.CreateCommand(procName, prams).ExecuteReader(CommandBehavior.CloseConnection);
        }

        public int RunProc(string procName, int timeout, SqlParameter[] prams)
        {
            int num;
            try
            {
                SqlCommand command = this.CreateCommand(procName, prams);
                command.CommandTimeout = timeout;
                command.ExecuteNonQuery();
                SqlParameter parameter = command.Parameters["ReturnValue"];
                if ((parameter != null) && (parameter.Value != null))
                {
                    return (int)parameter.Value;
                }
                num = 0;
            }
            finally
            {
                this.Close();
            }
            return num;
        }

        public int RunProc(string procName, SqlParameter[] prams, out int recordsAffected)
        {
            int num;
            try
            {
                SqlCommand command = this.CreateCommand(procName, prams);
                recordsAffected = command.ExecuteNonQuery();
                num = (int)command.Parameters["ReturnValue"].Value;
            }
            finally
            {
                this.Close();
            }
            return num;
        }

        public int RunProc(string procName, SqlParameter[] prams, bool mustClose)
        {
            int num;
            try
            {
                SqlCommand command = this.CreateCommand(procName, prams);
                command.ExecuteNonQuery();
                num = (int)command.Parameters["ReturnValue"].Value;
            }
            finally
            {
                if (mustClose)
                {
                    this.Close();
                }
            }
            return num;
        }

        public bool RunTransaction(SqlCommand[] commands)
        {
            bool flag;
            SqlTransaction transaction = this.cn.BeginTransaction();
            try
            {
                foreach (SqlCommand command in commands)
                {
                    command.ExecuteNonQuery();
                }
                transaction.Commit();
                flag = true;
            }
            catch (Exception)
            {
                try
                {
                    transaction.Rollback();
                }
                catch (Exception)
                {
                    return false;
                }
                flag = false;
            }
            finally
            {
                if (transaction != null)
                {
                    transaction.Dispose();
                }
            }
            return flag;
        }

        public virtual bool Disposed
        {
            get
            {
                return this._disposed;
            }
            set
            {
                this._disposed = value;
            }
        }

        public string ProcName
        {
            get
            {
                return this.procName;
            }
            set
            {
                this.procName = value;
            }
        }
    }
}

谢谢!

0 个答案:

没有答案
相关问题