Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I'm developing the master detail data entry form without using entity framwork. I have two tables deal making and deal details for this purpose I pass the values of master and detail to the controller using ajax call. its going well and all records are coming in the controller I check it by putting a breakpoint. but the issue is this it's not going inside while checking model state. It gives model stat false and giving the error of " Results View = Expanding the Results View will enumerate the IEnumerable."

What I have tried:

//ajax call in view
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; }
}
Posted
Comments
Vincent Maverick Durano 22-Sep-19 14:10pm    
Show us how your model ClsDealMaking is created.
Samps Pro 22-Sep-19 16:17pm    
its my model
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; }
}
Vincent Maverick Durano 22-Sep-19 22:53pm    
I can't seem to find any validation rules applied to your model. IsValid should return true for that.
Richard Deeming 23-Sep-19 9:19am    
"Expanding the Results View will enumerate the IEnumerable"

That's not an error. That's just telling you that if you expand the results view, it will have to iterate over the results. In some circumstances, this could change the state of the program, which would cause problems. But in this case, it won't cause any problems.

Expand the results view an examine the actual model state errors to see what the problem is.
Samps Pro 24-Sep-19 17:27pm    
the problem is im sending the array to the model also so its give me error
so its tell me im nt using correct model trpe to receive collection

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900