How to add ViewBag numbers which from different Model - c#

I have some ViewBag codes:
<p>#(((decimal)ViewBag.InvHardwareTotalPrice).ToString("C2"))</p>
<p>#(((decimal)ViewBag.InvSoftwareTotalPrice).ToString("C2"))</p>
<p>#(((decimal)ViewBag.InvFurnitureTotalPrice).ToString("C2"))</p>
They shows the price of items in webpage:
enter image description here
I want have a space to add the total number of those 3 numbers, I tried but not working:
<p>#((decimal)ViewBag.InvFurnitureTotalPrice) + #((decimal)ViewBag.InvFurnitureTotalPrice)</p>
Any ideas? Thank you!

Try it like this...
<p>
#{
decimal totalPrice = ((decimal)ViewBag.InvHardwareTotalPrice) + ((decimal)ViewBag.InvSoftwareTotalPrice) + ((decimal)ViewBag.InvFurnitureTotalPrice);
#totalPrice.ToString("C2")
}
</p>

Related

How to assign an ID value to each added row in a .txt file (ASP.NET MVC / C#)

I currently have a program where a user enters a text value into a text input, this text value is then written to a .txt file ("Category.txt") saved withing app_data. I need to assign an ID value (0, 1, 2, 3... etc.) to each user text input within this .txt file. Therefore an example row within the .txt file would be something like:
apple, 0
banana, 1
peach, 2
lemon, 3 ..
and so forth. (The ID's must be hidden from the user, as these are automatically assigned upon submitting of the text input).
I am completely lost on how i would go about doing this, the program needs to be written in C# code, so far i have this:
[HttpPost]
public ActionResult Category(string categoryDescription)
{
var dataFile = Server.MapPath("~/App_Data/Category.txt");
var indexLine = System.IO.File.ReadLines(dataFile).Last();
var textFileData = categoryDescription + " " + indexLine + Environment.NewLine;
System.IO.File.AppendAllText(dataFile, textFileData);
return View();
}
I am really quite unsure on what could be used to count the rows as new content is submitted.. Any help would be greatly appreciated, thanks.
I found a pretty easy way of doing this using the following code;
public ActionResult Category(string categoryDescription)
{
var dataFile = Server.MapPath("~/App_Data/Category.txt");
var numberOfLines = System.IO.File.ReadLines(dataFile).Count();
var textFileData = categoryDescription + "," + numberOfLines + Environment.NewLine;
System.IO.File.AppendAllText(dataFile, textFileData);
return View();
}
I basically just assigned the number of rows to a variable which will obviously accumulate as the user submits more and more text inputs.

Anchor and passing multiple query strings

I am trying to send an email with a link that contains multiple query strings. However i am getting an error that ";" expected near Eval.
bodyText is the body of the email that i am trying to send.
This is what i have tried.
bodyText = bodyText + "Please Click '<a href=http://urlpath/Default.aspx?param1=<%#Eval("p1")%>&param2=<%#Eval("p2")%>'Here</a> to view results"
Try this:
bodyText += <%# String.Format("Please Click Here to view results", Eval("p1"), Eval("p2")) %>
Use String.Format() for clarity and mind double quotes for HTML attributes.
not sure if it the problem but your tag is not formated correctly
should be like
bodyText = bodyText + "Please Click '<a href='http://urlpath/Default.aspx?param1=<%#Eval("p1")%>&param2=<%#Eval("p2")%>'>Here</a> to view results"
You have to escape the embedded double quote characters:
bodyText = bodyText + "Please Click '<a href=http://urlpath/Default.aspx?param1=<%#Eval(\"p1\")%>&param2=<%#Eval(\"p2\")%>'Here</a> to view results"

How to add a readmore option in gridview?

I am using asp.net with c#.
I am displaying some data from a database table. A column of that table is contains the description for that particular record. I want to show the minimum description first and also want to add some sort of read more options (Like, "Read more. . . " or ". . . ").
How can i do this job?
Help shell highly appreciated. . .
you can use this code :
public static string ShortDescription(string Description)
{
string result = Description;
if (result.Length > 50)
{
result = result.Substring(0, 50);
result += "....";
}
return result;
}
and use above method:
<p>
<%# ShortDescription(Eval("Description").ToString())%>
<a href='ShowDescription.aspx?Id=<%# Eval("Id") %>'>Read more. . . </a>
</p>
You can use jquery plugin - it was excellent for me -
http://dotdotdot.frebsite.nl/
For a pure client-side JavaScript solution you can hide part of the description in an invisible span:
This is the beginning
<span style="cursor:pointer" onclick="this.nextSibling.style.display = this.nextSibling.style.display == 'none'? '': 'none'">...more...</span>
<span style="display:none">of a very very very very very very very very very very very very very long description</span>
http://jsfiddle.net/J3Mxs/
Note that using this solution long description will display in the same cell as the original short one.

Trying to pad decimal number with leading spaces

for (int iCount = 0; iCount < oForm.LineItems.Count; iCount++)
{
// cartDetails is a stringbuilder here.
cartDetails.Append(String.Format("{0:0}", oForm.LineItems[iCount].Quantity));
cartDetails.Append(String.Format("{0:0.00}", oForm.LineItems[iCount].Price));
cartDetails.Append(String.Format("{0:0.00}", oForm.LineItems[iCount].ExtendedPrice));
//cartDetails.Append(string.Format("{0,10:#,##0.00}", oForm.LineItems[iCount].Price) + "</TD><TD>");
//cartDetails.Append(string.Format("{0,10:#,##0.00}", oForm.LineItems[iCount].ExtendedPrice) + "</TD><TD>");
//cartDetails.Append(String.Format("{0}", oForm.LineItems[iCount].Quantity).PadLeft(4)+ "</TD><TD>");
//cartDetails.Append(String.Format("{0:0.00}", oForm.LineItems[iCount].Price).PadLeft(8) + "</TD><TD>");
I have pastd the source code I am using. I add qty, price, extendedprice and all are decimal columns. All I am looking to do is to pad left with leading spaces. Decimal rounding to 2 digits seems to be happening.
Those commented lines above are some of the other options I have tried.
Currently if qty has values such as 4 and 40, they don't get aligned when I print them in a table. Same with price.
CAn someone please suggest what am I doing here?
Update1: Tried Lucas suggestion, but it is not working. Here is what I am geting.
cartDetails.Append(String.Format("{0:0,10}", oForm.LineItems[iCount].Quantity));
When I try the above, it shows 10 for every line irrespective of the value in oForm.LineItems[iCount].Quantity.
And if I change
String.Format("{0:0,4}", it shows 04 for all the records
You can use AppendFormat method instead of appending formatted string.
Also correct format will be {index,padding:format}. And consider to use foreach instead of for:
foreach (var lineItem in oForm.LineItems)
{
cartDetails.AppendFormat("{0,4:0}", lineItem.Quantity);
cartDetails.AppendFormat("{0,10:0.00}", lineItem.Price);
// etc
}
Remark: This is for alignemend in caracter based representation such as text files
Have a look at the last section in composite formatting (MSDN).
First format the number as desired and the pad the result
cartDetails.AppendFormat("{0,4}", // padding with spaces
String.Format("{0:0}", oForm.LineItems[iCount].Quantity)); // format number
Addtition: If you want to position your data in a html table you should use css (or inline styles)
<td class="right">This is right aligned</td>
with css
.right { text-align: right; }
or inlined:
<td style="text-align: right">This is right aligned</td>

Grab some text from a markup string using jQuery

I have the following markup:
<span>
Some text blalablalalbal<br/><br/>
"You are Awesome" <br/><br/>
----What can I do for you?<br/><br/>
</span>
Now I want to hide first line and modify the last line.
How can I grab those text using jQuery?
Note:
1: There are multiple instances of similar code-block but with different text. So I won't be able to hardcode. I am wondering if I can split it using tags somehow?
2: If it can be done in server-side code in C#, that is fine as well.
You can play around with textnodes doing something like this.
Update
var $nodes = $('span').contents().map(function(a,b){
return (b.nodeType===3?b:null);
});
// hide first line
$($nodes.get(0)).wrap('<span style="display:none;" />');
$nodes.get(2).data = "foo";
Working example of what I mean.
CSS has :first-line and :last-line modifiers.
Try:
span:first-line {
display: none;
}
Use jQuery with selector to modify last line:
var content = $('span:last-line').html();
content += " - Modified";
$('span:last-line').html(content);
You can split the line breaks and modify the array:
var elm = $('span'),
lines = elm.html().split('<br>');
lines.shift(); // removes the first
lines[lines.length-3] += '-modified'; // modify the last
elm.html(lines.join('<br>'));
Or you can put the three lines in three different spans with ids.
Something like:
<span>
<span id="sentence1">Some text blalablalalbal</span><br/><br/>
<span id="sentence2">"You are Awesome" </span><br/><br/>
<span id="sentence3">----What can I do for you?</span><br/><br/>
</span>
And hiding the first sentence with jquery:
$("span#sentence1").hide();

Categories