繁体   English   中英

将值从下拉列表传递到ASP.NET MVC中的数据库

[英]Passing values from dropDown list to Database in ASP.NET MVC

我有一个带有两个带有硬编码值的dropDown框的表单。 我需要将用户从视图中选择的值传递给控制器​​,并将其存储在数据库表中。

这是dropDown的HTML代码。

<div class="form-group">
  <label for="UserType">User Type:</label>
  <select name="usertype" class="form-control" id="drpUserType">
    <option value="0">Please Select</option>
    <option value="regular">Regular</option>
    <option value="preLoader">Pre Loader</option>
  </select>
</div>

这是脚本和AJAX。 这会将值传递给控制器​​。

$("#btnSubmitCustomer").click(function (){
  var customer = {};

  customer.ID = $("#hdnCustomerID").val();
  customer.UserType = $("#drpUserType").val();

  $.ajax({
          type: "POST",
          data: {customer : customer},
          dataType: "json",
          url: "@Url.Action("Save", "Customer")",
          success: function (response){
                if(response.type == "Success"){
                    notifyMe("Customer Added Successfully", "Success");
                    clearForm();
                    setDataTable();
                    $('#customerModal').modal('hide');
                }
            },
            error: function (error){
                console.log(error)
                $('#loading_spinner').hide();
            }
  });
});

这是控制器代码。

[HttpPost]
    public JsonResult Save(Customer customer)
    {
        bool userType;
        if (customer.UserType.Equals("preLoader"))
        {
            userType = customer.UserType == true;
        }
        else
        {
            userType = customer.UserType == false;
        }

        try
        {
            Customer cst = new Customer();
            using (MyDBContext myDBContext = new MyDBContext())
            {
                if (customer != null)
                {
                    DateTime localTime = DateTime.Now;

                    if (customer.ID == 0)
                    {
                        cst.UserType = userType;

                        myDBContext.Customer.Add(cst);
                    }
                    else
                    {
                        int customerID = customer.ID;
                        Customer customerOld = myDBContext.Customer.Where(x => x.ID == customerID).FirstOrDefault();
                        customerOld.UserType = customer.UserType;
                    }
                    myDBContext.SaveChanges();
                }
            }
            return Json(new { type = "Success", data = cst });
        }
        catch (Exception ex)
            {
            string message = ex.Message;
            if (ex.InnerException != null)
            {
                message += ex.InnerException.Message;
            }
            _Common.Log(message, "Customer", "save");
           return Json(new { type = "Error", message = ex.Message });
        }
    }

在MySql表中,userType是tinyint(1)。 我需要通过dropDown框验证用户类型,如果其“ preLoader | true”将其分配为“ 1”,如果其“常规| false”则将其分配为“ 0”。 但是无论用户选择的选项是什么,都会变得虚假。

我如何解决它?

传递数据时尝试使用此代码

var customer ={
ID = $("#hdnCustomerID").val();
UserType = $("#drpUserType").val();
};

在ajax中:

   $.ajax({
              ...
              data: JSON.stringify({ customer: customer }),
              ...
          });

暂无
暂无

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

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