使用ajax post插入数据 [英] Inserting data using ajax post

查看:60
本文介绍了使用ajax post插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表产品和类别。

我有一个包含产品价格,产品名称和CategoryName的表格dropDwon

当我尝试在数据库中插入数据时,CategoryName保存为null。



这是我用来保存数据库中数据的Add方法。

I have two tables Products and Categories .
I have a form with Product Price, Product Name and CategoryName dropDwon
When I try to Insert Data in the database , the CategoryName is saved as null.

This is Add method I have used to save the data in data base.

<pre>function Add() {
    var res = validate();
    if (res == false) {
        return false;
    }
    var proObj = {
        ProductName: $('#ProductName').val(),
        ProductPrice: $('#ProductPrice').val(),
        
        CategoryName: $('#CategoryName option:selected').val()
         //CategoryName is saved as null in db
      
    };
    
    
    $.ajax({
        url: "/AjaxProducts/Add",
        data: JSON.stringify(proObj),
        type: "POST",
        contentType: "application/json;charset=utf-8",
        
        dataType: "json",
        success: function (result) {
            //$("#ddlcountry").append($("<option></option>").val
            //(value.CountryId).html(value.CountryName));
            loadData();
            $('#myModal').modal('hide');
        },
        error: function (errormessage) {
            alert(errormessage.responseText);
        }
    });
}





我的尝试:



DropDownList



What I have tried:

DropDownList

<pre><div>
                        @Html.DropDownList("CategoryName", new SelectList(string.Empty, "Value", "Text"), "Please select a category",  new { @style = "width:250px;" })
                    </div>

推荐答案

' #ProductName')。val(),
ProductPrice:
('#ProductName').val(), ProductPrice:


' #ProductPrice')。val(),

CategoryName:
('#ProductPrice').val(), CategoryName:


' #CategoryName选项:选中')。val()
// CategoryName在db中保存为null

};
('#CategoryName option:selected').val() //CategoryName is saved as null in db };


这篇关于使用ajax post插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆