在MVC中将视图中的数组值从视图发送到控制器而无需实体框架

时间:2019-09-22 14:53:10

标签: javascript json asp.net-mvc asp.net-ajax ienumerable

`hello friends

我在不使用实体框架的情况下开发主要详细数据输入表单。 为此,我有两张桌子进行交易和交易详细信息     我使用ajax调用将master和detail的值传递给控制器​​。     它运行良好,所有记录都在控制器中,我通过放置一个断点来检查它。     但问题是,在检查模型状态时,它不在里面。它使模型状态为假,并给出错误    “结果视图=展开结果视图将枚举IEnumerable。”

please help me to resolve my issue

我的Ajax通话视图     创建交易明细对象

        var dealDetail = {  "ItemSysSeq": "", "Qty": "", "UnitPrice": "" };

        // creating deal master objects

        var DealMaster = { "DealSysSeq": "", "DealCode": "", "DealName": "", "ActiveFromDate": "", "ActiveToDate": "", "TotalCost": "", "Discount": "", "Dealoffer": "", "Remarks": "", "DealDetails": [] };

        //  // Set Deals Master Value
        DealMaster.DealSysSeq = 0;
        DealMaster.DealCode = $("#DealCode").val();
        DealMaster.DealName = $("#DealName").val();
        DealMaster.ActiveFromDate = $("#dateFrom").val
        DealMaster.ActiveToDate = $("#dateTo").val();
        DealMaster.TotalCost = $("#lblTotal").val();
        DealMaster.Discount = $("#txtdiscount").val();
        DealMaster.Dealoffer = $("#txtoffer").val();
        DealMaster.Remarks = $("#txtremarks").val();


        //creating the array and set the deal detail values

        //$('#stockitemError').text('');
        var stockvalues = [];
        //var erroritemcount = 0;

        $('#Dealdetailitems  tr').each(function (tr) {

            //if (
            //    $('select.pc', this).val() == "0" ||
            //    $('.txtQty', this).val() == "" ||
            //    isNaN($('.txtQty', this).val()) ||
            //    $('.txtopeningBalance', this).val() == "" ||
            //    isNaN($('.txtopeningBalance', this).val())
            //)
            //{
            //    erroritemcount++;
            //    $(this).addClass('error');


            //}



            //else {


            dealDetail.ItemSysSeq = $('select.pc', this).val(),
            dealDetail.Qty = parseFloat($('.txtQty', this).val()),
            dealDetail.UnitPrice = parseFloat($('.txtUnitPrice', this).val())

            DealMaster.DealDetails.push(dealDetail);

            dealDetail = { "ItemSysSeq": "", "Qty": "", "UnitPrice": "" };

            //}

        });


        // Set Ajax Post

        $.ajax({

            url: '/DealMaking/Create',

            data: JSON.stringify(DealMaster),

            //traditional: true,

            type: "POST",

            dataType: 'JSON',

            contentType: "application/json;  charset=utf-8",

            success: function (result) {

                //check is successfully save to database

                if (result.Scccess == 1) {

                    //will send status from server side

                    alert('Successfully done.');
                }

                else {
                    alert(result.ex);
         }
       }
   });
    });
//my controller
 public JsonResult Create( ClsDealMaking clsDeal )
        {
            bool status = false;

            if (ModelState.IsValid)
            {

                using (SqlConnection sqlCon = new SqlConnection(obj_conn.Conn()))
                {

                    sqlCon.Open();
                    string query = "INSERT INTO [dbo].[DealMaking] (DealCode,DealName,ActiveFromDate,ActiveToDate,TotalCost,DiscountInPercentage,DealOffer,Remarks) ";
                    query = query + "OUTPUT inserted .itemSysSeq Values(@DealCode,@DealName,@ActiveFromDate,@ActiveToDate,@TotalCost,@DiscountInPercentage,@DealOffer,@Remarks)";
                    SqlCommand sqlCmd = new SqlCommand(query, sqlCon);
                    sqlCmd.Parameters.AddWithValue("@DealCode", clsDeal.DealCode);
                    sqlCmd.Parameters.AddWithValue("@DealName", clsDeal.DealName);
                    sqlCmd.Parameters.AddWithValue("@ActiveFromDate", (object)clsDeal.ActiveFromDate.ToShortDateString() ?? DBNull.Value);
                    sqlCmd.Parameters.AddWithValue("@ActiveToDate", (object)clsDeal.ActiveToDate.ToShortDateString() ?? DBNull.Value);
                    sqlCmd.Parameters.AddWithValue("@TotalCost", (object)clsDeal.TotalCost ?? DBNull.Value);
                    sqlCmd.Parameters.AddWithValue("@DiscountInPercentage", (object)clsDeal.Discount ?? DBNull.Value);
                    sqlCmd.Parameters.AddWithValue("@DealOffer", (object)clsDeal.Dealoffer ?? DBNull.Value);
                    sqlCmd.Parameters.AddWithValue("@Remarks", (object)clsDeal.Remarks ?? DBNull.Value);
                  //  sqlCmd.Parameters.AddWithValue("@DealImage", (object)clsDeal.DealImage ?? DBNull.Value);

                    clsDeal.DealSysSeq = (int)sqlCmd.ExecuteScalar();

                 }

            }

            }

            return Json(JsonRequestBehavior.AllowGet);
        }

and model is//

 public class ClsDealMaking
    {
        public int DealSysSeq { get; set; }
        public string DealCode { get; set; }
        public string DealName { get; set; }
        public DateTime ActiveFromDate { get; set; }
        public DateTime ActiveToDate { get; set; }
        public double TotalCost { get; set; }
        public double Discount { get; set; }
        public double Dealoffer { get; set; }
        public string Remarks { get; set; }
       // public Byte[] DealImage { get; set; }
        public virtual ICollection<clsDealDetail> DealDetails { get; set; }
    }

0 个答案:

没有答案