DropdownList从mvc中的数据库绑定

时间:2018-05-29 05:34:24

标签: c# asp.net sql-server asp.net-mvc-5

朋友们,我在Asp.net MVC中更新鲜。我使用MVC 5.我想用数据库绑定下拉列表。我尝试了很多。但没有正常工作。请任何人帮助我。我使用View Model上课。

这是我的视图模型

 public class CustomerViewModel 
 {

        public int OrganizationId { get; set; }
        public string OrganizationName { get; set; }
        public List<SelectListItem> OrganizationList{ get ; set ; }
 }

这是我的控制器

    [HttpGet]
    public ActionResult AddCustomer()
    {
        CustomerViewModel model = new CustomerViewModel();

        model.OrganizationList = clsCRMTransactions.GetAllMembers().Select(m => new SelectListItem
                                 {
                                     Text =  m.OrganizationName,
                                     Value = m.OrganizationId.ToString()
                                  }).ToList();


       return View(model);
    }

然后是我的Post方法

[HttpPost]
    public ActionResult AddCustomer(CustomerViewModel AddContactVM)
    {
        try
        {
            if (ModelState.IsValid)
            {

                if (clsCRMTransactions.AddCustomer(AddContactVM))
                {
                    ViewBag.Message = "Contact Details Added Successfully";
                    ModelState.Clear();
                }
            }
            return View();
        }
        catch
        {
            return View();
        }
    }

我的观点是

@model Ringlus.ViewModels.CustomerViewModel

<div class="ibox-content">

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        @Html.ValidationSummary(true)

        <div class="row">
            <div class="col-lg-6">

                <div class="form-group">

                    <div class="col-md-12">
                        First Name
                        @Html.EditorFor(model => model.FirstName, new { @class = "form-control" })

                        @Html.ValidationMessageFor(model => model.FirstName)
                    </div>
                </div>

            </div>

        </div>
        <div class="row">
            <div class="col-lg-6">
                <div class="form-group">

                    <div class="col-md-12">
                        Mobile No
                        @Html.EditorFor(model => model.Mobile)
                        @Html.ValidationMessageFor(model => model.Mobile)
                    </div>
                </div>
            </div>

        </div>
        <div class="row">
            <div class="col-lg-12">
                <div class="form-group">
                    @Html.DropDownListFor(m => m.OrganizationId, Model.OrganizationList, "--Select One--")
                </div>
            </div>
        </div>


        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-primary" />

            </div>
        </div>


    </div>
}

DAL班是

   public List<CustomerViewModel> GetAllMembers()
    {
        connection();
        SqlCommand cmd = new SqlCommand("sp_GetAllOrganizations", con);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataReader reader = cmd.ExecuteReader();
        List<CustomerViewModel> mbd = new List<CustomerViewModel>();

                try
                {
                    while (reader.Read())
                    {
                        CustomerViewModel mb = new CustomerViewModel();
                        mb.OrganizationId = (int)reader["OrganizationID"];
                        mb.OrganizationName = (string)reader["OrganizationName"];

                        mbd.Add(mb);
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
                finally
                {

                        con.Close();

                }

                return mbd;

    }

public bool AddCustomer(CustomerViewModel AddCustomerVM)
    {
        connection();
        SqlCommand cmd = new SqlCommand("sp_createContacts", con);
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.AddWithValue("@FirstName", AddCustomerVM.FirstName);
        cmd.Parameters.AddWithValue("@OrganizationId", AddCustomerVM.OrganizationId);
        cmd.Parameters.AddWithValue("@MobileNo", AddCustomerVM.Mobile);

        int i = cmd.ExecuteNonQuery();
        con.Close();

        if (i >= 1)
            return true;
        else
            return false;
    }

这将绑定我的下拉列表。但它显示错误

  

对象引用未设置为对象的实例

我在发布数据时在@Html.DropDownListFor(m => m.OrganizationId, Model.OrganizationList, "--Select One--")

。 请帮我解决这个问题。

0 个答案:

没有答案