如何使用带有ajax调用的formcollection。 [英] How to use formcollection with ajax call.
本文介绍了如何使用带有ajax调用的formcollection。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此处 我的控制器代码...
< pre lang = C# > [HttpPost]
public ActionResult Create(FormCollection集合,字符串命令,List& lt; Orderdetail& gt;值)
{
尝试
{
// if(ModelState.IsValid)
// {
列表< FILEDETAIL& gt; FILEDETAILs = new 列表& lt; FILEDETAIL& gt;();
for ( int i = 0 ; i& lt; Request.Files.Count; i ++)
{
var file = Request.Files [i];
if (file!= null & amp; amp; amp; file.ContentLength& amp ; gt; 0 )
{
var fileName = Path.GetFileName(file 。文件名);
FILEDETAIL fILEDETAIL = new FILEDETAIL()
{
FileName = fileName,
Extension = Path.GetExtension(fileName) ,
Id = Guid.NewGuid()
};
FILEDETAILs.Add(fILEDETAIL);
var path = Path.Combine(Server.MapPath(& quot;〜/ App_Data / Upload& quot;),fILEDETAIL.Id + fILEDETAIL.Extension);
file.SaveAs(path);
}
}
列表& lt; REQ_ITEM_DETAILS& gt; reqitem = new 列表& lt; REQ_ITEM_DETAILS& gt;();
for ( int i = 0 ; i& lt; Values.Count; i ++)
{
REQ_ITEM_DETAILS req = new REQ_ITEM_DETAILS()
{
CO_COMP_CODE =& quot; 0& quot;,
CO_REQ_ID = reqviewmodel.tBL_REQUISITION.CO_REQ_ID,
DESCRIPTION = Values [i] .itemName,
QUANTITY = Values [i] .quantity ,
RATE = Values [i] .rate,
TOTAL = Values [i] .TotalAmount,
UNIT_ID = Convert.ToInt32(Values [i] .unit)
};
reqitem.Add(req);
}
reqviewmodel.Addrequisition(collection,FILEDETAILs,Request.Files.Count,reqitem);
return RedirectToAction(& quot; Index& quot;);
// }
// return View();
}
catch (Exception ex)
{
return View();
}
} < / pre >
视图我使用此
< pre lang = JavaScript> $(&#39;#submit&#39;)。click(function(){
// 订单验证
var isAllValid = true ;
var params = $(&#39;#f11&#39;)。serializeArray();
/// /如果有效,则保存
如果(isAllValid){
var data = {
& quot; command& quot;:& quot; Insert& quot;,
// OrderDate:$(&#39; #orderDate&#39;)。val()。trim(),
/// /抱歉忘记添加说明字段
// 描述:$(&#39;#description&#39;)。val()。trim(),
& quot; Values& quot;:orderItems,
& quot; collection& quot;: params
}
// $(this).val(&#39;请稍候......&#39;);
$。 ajax({
url:&#39; / Requisition / Create&#39;,
键入:& quot; POST& quot;,
data:JSON.stringify(data),
dataType:& quot; JSON& quot;,
contentType:& quot; application / json& quot;,
成功:函数(d){
// 检查成功保存到数据库
如果(d.status == true ){
// 将从服务器端发送状态
alert(&#39;记录保存已成功...&#39;);
// window.location.href =&#39; / Budget / BudgetList&#39 ;;
// clear form
// orderItems = [];
// < span class =code-comment> $(&#39;#orderNo&#39;)。val(&#39;&#39;);
// $(&#39; #orderDate&#39;)。val(&#39;&#39;) ;
// $(&#39;#orderItems&#39;)。 empty();
}
else {
alert (安培;#39;&失败放大器;#39);
}
// $(&#39;#submit&#39;)。 val(&#39; Save&#39;);
},
错误:function(){
alert(&#39;错误。请再次尝试。&#39;);
// $(&#39;#submit&#39;)。val(&# 39;保存&#39;);
}
}); < / pre >
什么我试过了:
这是我的控制器代码...
[HttpPost ]
public ActionResult Create(FormCollection集合,字符串命令,List< Orderdetail>值)
{
尝试
{
// if(ModelState.IsValid)
// {
列表与LT; FILEDETAIL> FILEDETAILs = new 列表< FILEDETAIL>();
for ( int i = 0 ; i < Request.Files.Count; i ++)
{
var file = Request.Files [i];
if (file!= null && file.ContentLength > 0 )
{
var fileName = Path.GetFileName(file.FileName);
FILEDETAIL fILEDETAIL = new FILEDETAIL()
{
FileName = fileName,
Extension = Path.GetExtension(fileName) ,
Id = Guid.NewGuid()
};
FILEDETAILs.Add(fILEDETAIL);
var path = Path.Combine(Server.MapPath( 〜/ App_Data / Upload),fILEDETAIL.Id + fILEDETAIL.Extension);
file.SaveAs(path);
}
}
列表< REQ_ITEM_DETAILS> reqitem = new 列表< REQ_ITEM_DETAILS>();
for ( int i = 0 ; i< Values.Count; i ++)
{
REQ_ITEM_DETAILS req = new REQ_ITEM_DETAILS()
{
CO_COMP_CODE = 0,
CO_REQ_ID = reqviewmodel.tBL_REQUISITION.CO_REQ_ID,
DESCRIPTION = Values [i] .itemName,
QUANTITY = Values [i] .quantity,
RATE = Values [i] .rate,
TOTAL = Values [i] .TotalAmount,
UNIT_ID = Convert.ToInt32(Values [i] .unit)
};
reqitem.Add(req);
}
reqviewmodel.Addrequisition(collection,FILEDETAILs,Request.Files.Count,reqitem);
return RedirectToAction( 索引跨度>);
// }
// return View();
}
catch (Exception ex)
{
return View();
}
}
查看我使用此
$(' #submit')。click( function (){
// 订单验证
var isAllValid = true ;
var params = $(' #f11')。serializeArray();
< span class =code-comment> // //保存(如果有效)
if (isAllValid){
var data = {
command: 插入,
// OrderDate:$('#orderDate')。val()。trim(),
// //抱歉忘了添加说明字段
// 描述:$('#description')。val()。trim(),
< span class =code-string>值:orderItems,
collection:params
}
// $(this).val('Please wait ...');
$ .ajax({
url:' /申请/创建',
类型: POST,
数据: JSON .stringify(data),
dataType: JSON,
contentType: application / json,
成功:功能(d){
// < span class =code-comment>检查成功保存到数据库
如果(d.status == true ){
// 将从服务器端发送状态
alert(' 记录保存已成功...');
// window.location.href ='/ Budget / BudgetList';
// clear form
// orderItems = [];
// $('#orderNo')。val('');
// $('#orderDate')。val('');
// $( '#orderItems')。empty();
}
else {
alert(' 失败');
}
// $('#submit')。val('Save');
},
错误: function (){
alert( ' 错误。请再试一次。');
// $('#submit')。val('Save');
}
});
解决方案
(&#39;#submit&#39;)。click (function(){
// 订单验证
var isAllValid = true ;
var params =
(&#39;#f11&#39;)。serializeArray();
/// /如果有效,则保存
如果(isAllValid){
var data = {
& quot; command& quot;:& quot; Insert& quot;,
// OrderDate:
(&#39; #orderDate&#39;)。val()。trim(),
/// /抱歉忘了添加说明字段
// 说明
here is my controller code ...
<pre lang="C#">[HttpPost]
public ActionResult Create(FormCollection collection,string command,List<Orderdetail> Values)
{
try
{
//if (ModelState.IsValid)
//{
List<FILEDETAIL> FILEDETAILs = new List<FILEDETAIL>();
for (int i = 0; i < Request.Files.Count; i++)
{
var file = Request.Files[i];
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
FILEDETAIL fILEDETAIL = new FILEDETAIL()
{
FileName = fileName,
Extension = Path.GetExtension(fileName),
Id = Guid.NewGuid()
};
FILEDETAILs.Add(fILEDETAIL);
var path = Path.Combine(Server.MapPath("~/App_Data/Upload"), fILEDETAIL.Id + fILEDETAIL.Extension);
file.SaveAs(path);
}
}
List<REQ_ITEM_DETAILS> reqitem = new List<REQ_ITEM_DETAILS>();
for (int i=0;i<Values.Count;i++ )
{
REQ_ITEM_DETAILS req = new REQ_ITEM_DETAILS()
{
CO_COMP_CODE="0",
CO_REQ_ID=reqviewmodel.tBL_REQUISITION.CO_REQ_ID,
DESCRIPTION=Values[i].itemName,
QUANTITY=Values[i].quantity,
RATE=Values[i].rate,
TOTAL=Values[i].TotalAmount,
UNIT_ID=Convert.ToInt32(Values[i].unit)
};
reqitem.Add(req);
}
reqviewmodel.Addrequisition(collection, FILEDETAILs, Request.Files.Count,reqitem);
return RedirectToAction("Index");
//}
//return View();
}
catch(Exception ex)
{
return View();
}
}</pre>
on view i use this
<pre lang="JavaScript">$('#submit').click(function () {
//validation of order
var isAllValid = true;
var params = $('#f11').serializeArray();
////Save if valid
if (isAllValid) {
var data = {
"command": "Insert",
//OrderDate: $('#orderDate').val().trim(),
////Sorry forgot to add Description Field
//Description: $('#description').val().trim(),
"Values": orderItems,
"collection": params
}
//$(this).val('Please wait...');
$.ajax({
url: '/Requisition/Create',
type: "POST",
data: JSON.stringify(data),
dataType: "JSON",
contentType: "application/json",
success: function (d) {
//check is successfully save to database
if (d.status == true) {
//will send status from server side
alert('Record Save has been Successfully...');
//window.location.href = '/Budget/BudgetList';
//clear form
//orderItems = [];
//$('#orderNo').val('');
//$('#orderDate').val('');
//$('#orderItems').empty();
}
else {
alert('Failed');
}
//$('#submit').val('Save');
},
error: function () {
alert('Error. Please try again.');
//$('#submit').val('Save');
}
});</pre>
What I have tried:
here is my controller code ...
[HttpPost]
public ActionResult Create(FormCollection collection,string command,List<Orderdetail> Values)
{
try
{
//if (ModelState.IsValid)
//{
List<FILEDETAIL> FILEDETAILs = new List<FILEDETAIL>();
for (int i = 0; i < Request.Files.Count; i++)
{
var file = Request.Files[i];
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
FILEDETAIL fILEDETAIL = new FILEDETAIL()
{
FileName = fileName,
Extension = Path.GetExtension(fileName),
Id = Guid.NewGuid()
};
FILEDETAILs.Add(fILEDETAIL);
var path = Path.Combine(Server.MapPath("~/App_Data/Upload"), fILEDETAIL.Id + fILEDETAIL.Extension);
file.SaveAs(path);
}
}
List<REQ_ITEM_DETAILS> reqitem = new List<REQ_ITEM_DETAILS>();
for (int i=0;i<Values.Count;i++ )
{
REQ_ITEM_DETAILS req = new REQ_ITEM_DETAILS()
{
CO_COMP_CODE="0",
CO_REQ_ID=reqviewmodel.tBL_REQUISITION.CO_REQ_ID,
DESCRIPTION=Values[i].itemName,
QUANTITY=Values[i].quantity,
RATE=Values[i].rate,
TOTAL=Values[i].TotalAmount,
UNIT_ID=Convert.ToInt32(Values[i].unit)
};
reqitem.Add(req);
}
reqviewmodel.Addrequisition(collection, FILEDETAILs, Request.Files.Count,reqitem);
return RedirectToAction("Index");
//}
//return View();
}
catch(Exception ex)
{
return View();
}
}
on view i use this
$('#submit').click(function () {
//validation of order
var isAllValid = true;
var params = $('#f11').serializeArray();
////Save if valid
if (isAllValid) {
var data = {
"command": "Insert",
//OrderDate: $('#orderDate').val().trim(),
////Sorry forgot to add Description Field
//Description: $('#description').val().trim(),
"Values": orderItems,
"collection": params
}
//$(this).val('Please wait...');
$.ajax({
url: '/Requisition/Create',
type: "POST",
data: JSON.stringify(data),
dataType: "JSON",
contentType: "application/json",
success: function (d) {
//check is successfully save to database
if (d.status == true) {
//will send status from server side
alert('Record Save has been Successfully...');
//window.location.href = '/Budget/BudgetList';
//clear form
//orderItems = [];
//$('#orderNo').val('');
//$('#orderDate').val('');
//$('#orderItems').empty();
}
else {
alert('Failed');
}
//$('#submit').val('Save');
},
error: function () {
alert('Error. Please try again.');
//$('#submit').val('Save');
}
});
解决方案
('#submit').click(function () { //validation of order var isAllValid = true; var params =
('#f11').serializeArray(); ////Save if valid if (isAllValid) { var data = { "command": "Insert", //OrderDate:
('#orderDate').val().trim(), ////Sorry forgot to add Description Field //Description:
这篇关于如何使用带有ajax调用的formcollection。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文