我正在研究一种生成pdf的方法。为此,我使用tuespechkin库。转换本身就相当快。大部分时间需要从数据库中检索数据并创建pdf的html表示。为了提高性能,我尝试使用异步方法和PLinq,但它没有多大帮助。有没有办法优化这个方法?
代码中最昂贵的部分(更新):
StringBuilder html = new StringBuilder(1024 * 1024); // adapt memory reservation depending on document size
//---------------------------- Start page -----------------------------------------------------------//
imageArray = System.IO.File.ReadAllBytes(HttpContext.Server.MapPath("../Content/images/pdf_logo.jpg"));
base64ImageRepresentation = Convert.ToBase64String(imageArray);
html.Append("<div class='break' style='margin: 0 0 0 0px;'>");
html.Append("<img class='report-img' style='margin:150px 0 400px 0;' src='data:image/jpeg;base64," + base64ImageRepresentation + "'></img>");
html.Append("<p class='purpleText' style='color:#8E0033; font-size: 30px; margin:0; line-height:20px;'>" + title + "</p>" +
"<p class='purpleText' style='color:#8E0033; font-size: 16px;'>" + bigTitle + "</p>");
html.Append("<p style='color:#980000; font-size: 50px; margin: 46px 0 0 0;'>" + custName.firstName + " <span>" + custName.lastName + "</span></p>");
html.Append("</div>");
//------------------------------------ 2nd page -----------------------------------------------------------//
html.Append("<div class='text-block break'></div>");
//=======================================================================================================================//
//------------------------------------- Generate menu (p4) ----------------------------------------------------------//
html.Append(header);
html.Append("<span class='menu-title'>INNHOLDSFORTEGNELSE</span>");
html.Append("<ul class='content-list break'>");
//------------------ Innlednings menu --------------------------//
if (await db.Innlednings.AnyAsync(n => n.CustomerId == customerId))
{
html.Append("<li class='list-header'>Innledning</li>");
}
//------------------ Fordelings menu --------------------------//
if (await db.Fordelings.AnyAsync(n => n.CustomerId == customerId))
{
html.Append("<li class='list-header'>Fordeling av bruk og regninger</li>");
}
//--------------------------------- Report Area Bruk Menu ----------------------------------------------------//
//--------------------------- Get transaction category id of selected area ---------------------------//
const string tranCatBruk = "Bruk";
var tranCatIdBruk = 0;
if (await db.TransactionCategories.AnyAsync(n => n.Name == tranCatBruk))
{
tranCatIdBruk = (await (db.TransactionCategories.FirstAsync(n => n.Name == tranCatBruk))).Id;
}
else
{
throw new HttpException(404, "'" + tranCatBruk + "' transaction category does not exist!");
}
if (await db.ReportViews.AnyAsync(n => n.CustomerId == customerId && n.TransactionCategoryId == tranCatIdBruk))
{
html.Append("<li class='list-header'>Brukskonto</li>");
if (await db.FordelingBruks.AnyAsync(n => n.CustomerId == customerId))
{
html.Append("<li class='list-level2'>Fordeling av brukskonto</li>");
}
var rvBrukItems = await
db.ReportViews.Where(n => n.CustomerId == customerId && n.TransactionCategoryId == tranCatIdBruk).ToListAsync();
foreach (var rvItem in rvBrukItems)
{
var mainCatName = (await db.Categories.FirstAsync(n => n.Id == rvItem.MainCategoryId)).Name;
if (!rvItem.TotalPerMonthImage.IsNullOrWhiteSpace() ||
!rvItem.DifferentStoresImage.IsNullOrWhiteSpace() ||
!rvItem.SubCategoryImage.IsNullOrWhiteSpace() ||
!rvItem.TransactionsPerMonthImage.IsNullOrWhiteSpace() ||
!rvItem.AverageTradeValueImage.IsNullOrWhiteSpace())
{
html.Append("<li class='list-level2'>" + mainCatName + "</li>");
}
//----------------- get images --------------------------//
if (!rvItem.TotalPerMonthImage.IsNullOrWhiteSpace())
{
html.Append("<li class='list-level3'>Totalt pr. måned</li>");
}
if (!rvItem.DifferentStoresImage.IsNullOrWhiteSpace())
{
html.Append("<li class='list-level3'>Fordeling</li>");
}
if (!rvItem.SubCategoryImage.IsNullOrWhiteSpace())
{
html.Append("<li class='list-level3'>Kategorier</li>");
}
if (!rvItem.TransactionsPerMonthImage.IsNullOrWhiteSpace())
{
html.Append("<li class='list-level3'>Transaksjoner pr. måned</li>");
}
if (!rvItem.AverageTradeValueImage.IsNullOrWhiteSpace())
{
html.Append("<li class='list-level3'>Gjennomsnittlig handlebeløp</li>");
}
}
}
//--------------------------------- Report Area Regning Menu ----------------------------------------------------//
//--------------------------- Get transaction category id of selected area ---------------------------//
const string tranCatRegning = "Regning";
var tranCatIdRegning = 0;
if (await db.TransactionCategories.AnyAsync(n => n.Name == tranCatRegning))
{
tranCatIdRegning = (await db.TransactionCategories.FirstAsync(n => n.Name == tranCatRegning)).Id;
}
else
{
throw new HttpException(404, "'" + tranCatRegning + "' transaction category does not exist!");
}
if (await db.ReportViews.AnyAsync(n => n.CustomerId == customerId && n.TransactionCategoryId == tranCatIdRegning))
{
html.Append("<li class='list-header'>Regningskonto</li>");
if (await db.FordelingBruks.AnyAsync(n => n.CustomerId == customerId))
{
html.Append("<li class='list-level2'>Fordeling av regningskonto</li>");
}
var rvBrukItems = await
db.ReportViews.Where(n => n.CustomerId == customerId && n.TransactionCategoryId == tranCatIdRegning).ToListAsync();
foreach (var rvItem in rvBrukItems)
{
var mainCatName = (await db.Categories.FirstAsync(n => n.Id == rvItem.MainCategoryId)).Name;
if (!rvItem.TotalPerMonthImage.IsNullOrWhiteSpace() ||
!rvItem.DifferentStoresImage.IsNullOrWhiteSpace() ||
!rvItem.SubCategoryImage.IsNullOrWhiteSpace() ||
!rvItem.TransactionsPerMonthImage.IsNullOrWhiteSpace() ||
!rvItem.AverageTradeValueImage.IsNullOrWhiteSpace())
{
html.Append("<li class='list-level2'>" + mainCatName + "</li>");
}
//----------------- get images --------------------------//
if (!rvItem.TotalPerMonthImage.IsNullOrWhiteSpace())
{
html.Append("<li class='list-level3'>Totalt pr. måned</li>");
}
if (!rvItem.DifferentStoresImage.IsNullOrWhiteSpace())
{
html.Append("<li class='list-level3'>Fordeling</li>");
}
if (!rvItem.SubCategoryImage.IsNullOrWhiteSpace())
{
html.Append("<li class='list-level3'>Kategorier</li>");
}
if (!rvItem.TransactionsPerMonthImage.IsNullOrWhiteSpace())
{
html.Append("<li class='list-level3'>Transaksjoner pr. måned</li>");
}
if (!rvItem.AverageTradeValueImage.IsNullOrWhiteSpace())
{
html.Append("<li class='list-level3'>Gjennomsnittlig handlebeløp</li>");
}
}
}
//------------------ Inntekt menu --------------------------//
html.Append("<li class='list-header'>Inntekt</li>");
if (await db.InntektPerMonths.AnyAsync(n => n.CustomerId == customerId))
{
html.Append("<li class='list-level2'>Inntekt per måned</li>");
}
if (await db.InntektSubCategories.AnyAsync(n => n.CustomerId == customerId))
{
html.Append("<li class='list-level2'>Fordeling av inntekt</li>");
}
if (await db.InntektFordelings.AnyAsync(n => n.CustomerId == customerId))
{
html.Append("<li class='list-level2'>Fordeling mellom inntekt og forbruk</li>");
}
//----------------- SPARING menu ------------------------//
if (db.SparingKommentarers.Any(n => n.CustomerId == customerId) ||
db.SparingTabells.Any(n => n.CustomerId == customerId))
{
html.Append("<li class='list-header'>Sparing</li>");
if (await db.SparingKommentarers.AnyAsync(n => n.CustomerId == customerId))
{
html.Append("<li class='list-level2'>Kommentar</li>");
}
if (await db.SparingRads.AnyAsync(n => n.CustomerId == customerId))
{
html.Append("<li class='list-level2'>Sparetips</li>");
}
if (await db.SparingTabells.AnyAsync(n => n.CustomerId == customerId))
{
html.Append("<li class='list-level2'>Tabell</li>");
}
}
//----------------- BUDSJETT menu ------------------------//
if (db.Budsjettkommentarers.Any() ||
db.BudsjettGraf1s.Any() ||
db.TotalBruks.Any() ||
db.TotalRegningers.Any())
{
html.Append("<li class='list-header'>Budsjett</li>");
if (await db.Budsjettkommentarers.AnyAsync(n => n.CustomerId == customerId))
{
html.Append("<li class='list-level2'>Kommentar</li>");
}
if (await db.TotalBruks.AnyAsync(n => n.CustomerId == customerId))
{
html.Append("<li class='list-level2'>Totalt bruk</li>");
}
if (await db.TotalRegningers.AnyAsync(n => n.CustomerId == customerId))
{
html.Append("<li class='list-level2'>Total regninger</li>");
}
if (await db.BudsjettGraf1s.AnyAsync(n => n.CustomerId == customerId))
{
html.Append("<li class='list-level2'>Budsjett tall</li>");
}
}
//----------------- OPPSUMMERING(Articles) menu ------------------------//
if (await db.Articles.AnyAsync(n => n.CustomerId == customerId))
{
html.Append("<li class='list-header'>Oppsummering</li>");
}
//------------------ End of Menu -----------------------------------------------//
html.Append("</ul>");
//------------------ two blank pages (6-7) ---------------------------------------//
////------------------------------- Start generating inner pdf ---------------------------------------------//
////---------------------------------------Innledning------------------------------------------------------//
var customerforInnlending = await db.Innlednings.FirstOrDefaultAsync(inl => inl.CustomerId == customerId);
html.Append(header);
string textforInnlending = (customerforInnlending != null) ? customerforInnlending.Text : "<b>Empty Text</b>";
html.Append("<h1 class='menu-title'>Innledning </h1>" +
"<div class='text-block'>" + textforInnlending + "</div>" +
"<p class='break'></p>");
html.Append("<br>");
//-----------------------------------Fordeling-----------------------------------------------------------//
var fordelingList = await db.Fordelings.Where(cus => cus.CustomerId == customerId).ToListAsync();
foreach (var item in fordelingList)
{
if (!item.TransactionsImage.IsNullOrWhiteSpace())
{
html.Append(header);
html.Append("<h1> Fordeling av bruk og regninger </h1>");
html.Append("<img class='report-img circlediagram' width='800px' height='auto' src='" + item.TransactionsImage + "'></img>");
html.Append("<div class='text-block'>" + item.Text + "</div>");
}
if (!item.TopTransactionsImage.IsNullOrWhiteSpace())
{
html.Append("<img class='report-img stolpeddiagram mtop30' width='600px' height='auto' src='" + item.TopTransactionsImage + "'></img>");
}
html.Append("<div class='text-block break'></div>");
}
//---------------------------- Bruskonto title -----------------------------------------------------------//
imageArray = System.IO.File.ReadAllBytes(HttpContext.Server.MapPath("../Content/images/bruk_header.png"));
base64ImageRepresentation = Convert.ToBase64String(imageArray);
html.Append(header);
html.Append("<div class='break' style='display:block;padding-top:300px;padding-left:0px;'>");
html.Append("<img class='report-img' src='data:image/png;base64," + base64ImageRepresentation + "'></img>");
html.Append("</div>");
////------------------------------------------------Fordeling av Bruk------------------------------------------------------//
var fordelingBruk = await db.FordelingBruks.Where(cus => cus.CustomerId == customerId).ToListAsync();
foreach (var item in fordelingBruk)
{
if (!item.MainCatImage.IsNullOrWhiteSpace())
{
html.Append(header);
html.Append("<h1>Fordeling av Brukskonto </h1>");
html.Append("<img class='report-img circlediagram' src='" + item.MainCatImage + "'></img>");
html.Append("<div class='text-block'>" + item.Text + "</div>");
}
if (!item.TopMainCatImage.IsNullOrWhiteSpace())
{
html.Append("<img class='report-img stolpeddiagram mtop30' width='600px' height='auto' src='" + item.TopMainCatImage + "'></img>");
}
html.Append("<div class='text-block break'></div>");
}
//------------------------------------------------ ReportView Bruk------------------------------------------------------//
//--------------------------- Get transaction category id of selected area ---------------------------//
var tranCat = "Bruk";
var tranCatId = 0;
if (await db.TransactionCategories.AnyAsync(n => n.Name == tranCat))
{
tranCatId = (await db.TransactionCategories.FirstAsync(n => n.Name == tranCat)).Id;
}
else
{
throw new HttpException(404, "'" + tranCat + "' transaction category does not exist!");
}
var reportViewBrukList = db.ReportViews.AsParallel().Where(n => n.TransactionCategoryId == tranCatId && n.CustomerId == customerId).ToList();
foreach (var rvItem in reportViewBrukList)
{
var mainCatName = (await db.Categories.FirstAsync(n => n.Parent == null && n.Id == rvItem.MainCategoryId)).Name;
//----------------- get images --------------------------//
if (!rvItem.TotalPerMonthImage.IsNullOrWhiteSpace())
{
html.Append(header);
html.Append("<h1> Diverse " + mainCatName + "</h1>");
html.Append("<img class='report-img img-size' src='" + rvItem.TotalPerMonthImage + "'></img>");
html.Append("<div class='text-block break'>" + rvItem.TotalPerMonthText + "</div>");
}
if (!rvItem.DifferentStoresImage.IsNullOrWhiteSpace())
{
html.Append(header);
html.Append("<h1> Fordeling av " + mainCatName + "</h1>");
html.Append("<img class='report-img img-size' src='" + rvItem.DifferentStoresImage + "'></img>");
html.Append("<div class='text-block break'>" + rvItem.DifferentStoresText + "</div>");
}
if (!rvItem.SubCategoryImage.IsNullOrWhiteSpace())
{
html.Append(header);
html.Append("<h1> Fordeling på " + mainCatName + "</h1>");
html.Append("<img class='report-img img-size' src='" + rvItem.SubCategoryImage + "'></img>");
html.Append("<div class='text-block break'>" + rvItem.SubCategoryText + "</div>");
}
if (!rvItem.TransactionsPerMonthImage.IsNullOrWhiteSpace())
{
html.Append(header);
html.Append("<h1>" + mainCatName + "</h1>");
html.Append("<img class='report-img img-size' src='" + rvItem.TransactionsPerMonthImage + "'></img>");
html.Append("<div class='text-block break'>" + rvItem.TransactionsPerMonthText + "</div>");
}
if (!rvItem.AverageTradeValueImage.IsNullOrWhiteSpace())
{
html.Append(header);
html.Append("<h1> Antall transaksjoner pr. mnd på " + mainCatName + "</h1>");
html.Append("<img class='report-img img-size' src='" + rvItem.AverageTradeValueImage + "'></img>");
html.Append("<div class='text-block break'>" + rvItem.AverageTradeValueText + "</div>");
}
}
//---------------------------- Regningskonto title -----------------------------------------------------------//
imageArray = System.IO.File.ReadAllBytes(HttpContext.Server.MapPath("../Content/images/regninger_header.png"));
base64ImageRepresentation = Convert.ToBase64String(imageArray);
html.Append(header);
html.Append("<div class='break' style='display:block;padding-top:300px;padding-left:0px;'>");
html.Append("<img class='report-img' src='data:image/png;base64," + base64ImageRepresentation + "'></img>");
html.Append("</div>");
//-------------------------------------------- Fordeling av regninger (page 39) ---------------------------------------------------------//
var fordelingRegninger = await db.FordelingRegningers.Where(cus => cus.CustomerId == customerId).ToListAsync();
foreach (var item in fordelingRegninger)
{
if (!item.MainCatImage.IsNullOrWhiteSpace())
{
html.Append(header);
html.Append("<h1>Fordeling av regninger </p>");
html.Append("<img class='report-img circlediagram' width='800px' height='auto' src='" + item.MainCatImage + "'></img>");
html.Append("<div class='text-block'>" + item.Text + "</div>");
}
if (!item.TopMainCatImage.IsNullOrWhiteSpace())
{
html.Append("<img class='report-img stolpeddiagram mtop30' width='600px' height='auto' src='" + item.TopMainCatImage + "'></img>");
}
html.Append("<div class='text-block break'></div>");
}
//------------------------------------------------ ReportView Regninger (p38-48)------------------------------------------------------//
//--------------------------- Get transaction category id of selected area ---------------------------//
tranCat = "Regning";
tranCatId = 0;
if (await db.TransactionCategories.AnyAsync(n => n.Name == tranCat))
{
tranCatId = (await db.TransactionCategories.FirstAsync(n => n.Name == tranCat)).Id;
}
else
{
throw new HttpException(404, "'" + tranCat + "' transaction category does not exist!");
}
var reportViewRegningerList = db.ReportViews.AsParallel().Where(n => n.TransactionCategoryId == tranCatId && n.CustomerId == customerId).ToList();
foreach (var rvItem in reportViewRegningerList)
{
var mainCatName = (await db.Categories.FirstAsync(n => n.Parent == null && n.Id == rvItem.MainCategoryId)).Name;
//----------------- get images --------------------------//
if (!rvItem.TotalPerMonthImage.IsNullOrWhiteSpace())
{
html.Append(header);
html.Append("<h1> Diverse " + mainCatName + "</h1>");
html.Append("<img class='report-img img-size' src='" + rvItem.TotalPerMonthImage + "'></img>");
html.Append("<div class='text-block break'>" + rvItem.TotalPerMonthText + "</div>");
}
if (!rvItem.DifferentStoresImage.IsNullOrWhiteSpace())
{
html.Append(header);
html.Append("<h1> Fordeling av " + mainCatName + "</h1>");
html.Append("<img class='report-img img-size' src='" + rvItem.DifferentStoresImage + "'></img>");
html.Append("<div class='text-block break'>" + rvItem.DifferentStoresText + "</div>");
}
if (!rvItem.SubCategoryImage.IsNullOrWhiteSpace())
{
html.Append(header);
html.Append("<h1> Fordeling på " + mainCatName + "</h1>");
html.Append("<img class='report-img img-size' src='" + rvItem.SubCategoryImage + "'></img>");
html.Append("<div class='text-block break'>" + rvItem.SubCategoryText + "</div>");
}
if (!rvItem.TransactionsPerMonthImage.IsNullOrWhiteSpace())
{
html.Append(header);
html.Append("<h1>" + mainCatName + "</h1>");
html.Append("<img class='report-img img-size' src='" + rvItem.TransactionsPerMonthImage + "'></img>");
html.Append("<div class='text-block break'>" + rvItem.TransactionsPerMonthText + "</div>");
}
if (!rvItem.AverageTradeValueImage.IsNullOrWhiteSpace())
{
html.Append(header);
html.Append("<h1> Antall transaksjoner pr. mnd på " + mainCatName + "</h1>");
html.Append("<img class='report-img img-size' src='" + rvItem.AverageTradeValueImage + "'></img>");
html.Append("<div class='text-block break'>" + rvItem.AverageTradeValueText + "</div>");
}
}
//---------------------------- Inntekter title -----------------------------------------------------------//
imageArray = System.IO.File.ReadAllBytes(HttpContext.Server.MapPath("../Content/images/inntekter_header.png"));
base64ImageRepresentation = Convert.ToBase64String(imageArray);
html.Append(header);
html.Append("<div class='break' style='display:block;padding-top:300px;padding-left:0px;'>");
html.Append("<img class='report-img' src='data:image/png;base64," + base64ImageRepresentation + "'></img>");
html.Append("</div>");
//---------------------------------------- Inntekt per måned (p50) --------------------------------------------------------------//
if (await db.InntektPerMonths.AnyAsync(n => n.CustomerId == customerId))
{
var innPerManed = await db.InntektPerMonths.FirstAsync(n => n.CustomerId == customerId);
if (!innPerManed.InntektPerMonthImage.IsNullOrWhiteSpace())
{
html.Append(header);
html.Append("<h1> Inntekt per måned </h1>");
html.Append("<img class='report-img img-size' src='" + innPerManed.InntektPerMonthImage + "'></img>");
html.Append("<div class='text-block break'>" + innPerManed.Text + "</div>");
}
}
//---------------------------------------- Fordeling av inntekt (p51) --------------------------------------------------------------//
if (await db.InntektFordelings.AnyAsync(n => n.CustomerId == customerId))
{
var innFordelings = await db.InntektSubCategories.FirstAsync(n => n.CustomerId == customerId);
if (!innFordelings.InntektSubCategoryImage.IsNullOrWhiteSpace())
{
html.Append(header);
html.Append("<h1> Fordeling av inntekt </h1>");
html.Append("<img class='report-img img-size' src='" + innFordelings.InntektSubCategoryImage + "'></img>");
html.Append("<div class='text-block break'>" + innFordelings.Text + "</div>");
}
}
//---------------------------------------- Fordeling mellom inntekt og forbruk (p52) --------------------------------------------------------------//
if (await db.InntektSubCategories.AnyAsync(n => n.CustomerId == customerId))
{
var innSub = await db.InntektFordelings.FirstAsync(n => n.CustomerId == customerId);
if (!innSub.InntektFordelingImage.IsNullOrWhiteSpace())
{
html.Append(header);
html.Append("<h1> Fordeling mellom inntekt og forbruk </h1>");
html.Append("<img class='report-img img-size' src='" + innSub.InntektFordelingImage + "'></img>");
html.Append("<div class='text-block break'>" + innSub.Text + "</div>");
}
}
html.Append("</body></html>");
//---------- some code
//........
var htmlStr = html.toString();
您可以做以下事情来提高性能:
var
替换为确切类型,匿名类型除外。 async/wait
(删除并观察它是否会影响性能),因为我不认为这在你的情况下是有益的。(这是可选的)Foreach
循环中取出。示例:
html.Append("<div class='break' style='margin: 0 0 0 0px;'><img class='report-img' style='margin:150px 0 400px 0;' src='data:image/jpeg;base64,")
.Append(base64ImageRepresentation)
.Append("</img><p class='purpleText' style='color:#8E0033; font-size: 30px; margin:0; line-height:20px;'>")
.Append( title)
.Append("</p><p class='purpleText' style='color:#8E0033; font-size: 16px;'>")
.Append( bigTitle)
.Append("</p><p style='color:#980000; font-size: 50px; margin: 46px 0 0 0;'>")
.Append(custName.firstName)
.Append("<span>")
.Append(custName.lastName)
.Append("</span></p></div>");
var
示例:var tranCatIdBruk=0
至
int-tranCatIdBruk=0
你在Foreach循环中使用了var关键字,这将导致性能下降(相信我,我已经在这方面做了实践,当时我很震惊: D)
foreach
外部变量示例:
var mainCatName; // Replace var with string.
foreach (var rvItem in rvBrukItems)
{
mainCatName = (await db.Categories.FirstAsync(n => n.Id == rvItem.MainCategoryId)).Name;
if (!rvItem.TotalPerMonthImage.IsNullOrWhiteSpace() ||
!rvItem.DifferentStoresImage.IsNullOrWhiteSpace() ||
!rvItem.SubCategoryImage.IsNullOrWhiteSpace() ||
!rvItem.TransactionsPerMonthImage.IsNullOrWhiteSpace() ||
!rvItem.AverageTradeValueImage.IsNullOrWhiteSpace())
{
html.Append("<li class='list-level2'>" + mainCatName + "</li>");
}
}
对于
StringBuilder Append()
,您可以参考下面的链接,该链接解释了内部发生的情况:
字符串生成器最佳实践
希望这对你有帮助。如果你还有其他疑问,请告诉我。
字符串在中是不可变的。网您应该使用StringBuilder
而不是字符串。
类似的东西:
StringBuilder sb = new StringBuilder(16 * 1024); // adapt memory reservation depending on document size
sb.Append("string_to_append");
...
return sb.ToString();
这种技术远比=
有效
问题内容: 我想使用$ .POST或$ .AJAX实现创建(插入)屏幕。 注意:代码无需AJAX调用即可正常工作..它已经存在..现在我需要进行ajax调用并保存而无需回发。我写了下面的代码: 在提交点击事件时,代码如下: 在服务器端: 问题是,每次它都会调用操作,但在参数param 上找不到数据。我也试图保持-视图模型作为参数,但是它没有用..只是给了我null值。同样,在ajax调用中,它成功
问题内容: 我一直在尝试整个下午在网络上爬行,试图在动作控制器中接收JSON对象。 什么是正确的或更简单的方法? 我尝试了以下方法:1: 这给了我输入一个空值。 2: 这在尝试发布到它的jQuery方面给了我500错误?(这表示未正确绑定)。 这是我的jQuery代码: 我要做的就是在我的mvc动作中接收JSON对象? 问题答案: 不幸的是,Dictionary在MVC中存在模型绑定问题。在这里阅
问题内容: 我试图实现对包含复杂对象作为参数的控制器操作方法的JQueryAJAX调用。我读了很多博客,并尝试了从中学习到的几种技巧。 我想触发一个异步发布,当用户在某个字段上制表符时调用该异步发布(不是表单保存发布–如我发现的其他示例所示)。 我的意图是: 在客户端上实例化一个对象(不是为View提供类型的ViewModel); 使用视图中多个字段中的数据填充对象; 将此对象转换为JSON; 使
问题内容: 我一直在这里看问题:将MVCajaxjson发布到控制器操作方法,但不幸的是,它似乎并没有帮助我。我的几乎完全相同,除了我的方法签名(但是我已经尝试过了,但仍然没有被击中)。 jQuery的 控制者 但是,无论我尝试什么,我都不会受到打击。通过调试,我知道它发送了一个请求,每次都会得到一个错误。 问题答案: 您的操作需要字符串参数,但是您要发送一个复合对象。 您需要创建一个与您要发送的
问题内容: 我已经尝试了几个小时才能使其正常工作,我真的希望你们中的一个比我更了解(很多)。当客户端在文本框中键入内容时,我想调用MVC C#控制器方法称为updateOrder()。理想情况下,我想使用FormCollection访问表单元素(该表单称为“ createOrder”)。 在控制器中,我有: C# 上面的字符串声明是断点的。在视图中,我有一个基本上可以复制并粘贴在stackover
问题内容: 我正在尝试使用以下方法将使用jQuery Ajax的数据发布到MVC动作。但是在控制器内部,所有模型属性始终都是。不知道我在这里想念的是什么。 .CSHTML 的JavaScript 控制者 模型 EDIT1 我已经添加了上面的模型。单击保存后,此处显示序列化数据和JSON 数据。 序列化数据 后 我尝试添加属性和没有运气的属性。 我认为不必将返回类型从更改为。 该URL也是正确的,因