truncate string in mvc homecontroller javascript - javascript

I have a string coming in from the database say, ExpectedDate. I get the string in as 2015-07-08T00:00:00. I want to be able to remove the T00:00:00.from the string. Is this possible in MVC from the homecontroller javascript? if so, how?
I've tried to use a replace function, but I can't find the replace method in the MVC javascript file. What am I doing wrong?
Please help!

Controllers are not generally written in JavaScript in .NET. If you're going to do it in JavaScript client side, it would be much easier to load it into a Date object and then use the toDateString method with a format:
var d = new Date("2015-07-08T00:00:00")
d.toDateString("yyyy-MM-dd");
If you have the .NET DateTime object before it writes to the client, then you can do
your_date_time_object.ToString("yyyy-MM-dd");

Related

How to convert javascript to HTML

I'm completely new here and this is my first question. I'm scared.
I will be honest, I'm completely new at HTML and javascript, and my doubt is the following: I'm performing an HttpWebRequest in order to get a string with some text in html format. This text contains lots of javascript code, but what I actually need is to get a string containing the interpreted html (this means, what a browser would do when executing that source code after having entered the corresponding URL in the browse bar).
Is there any way to make such a conversion in C#?
Html.Raw() can be,
in ASP.NET MVC Razor view
For example: Html.Raw("<div>").

Convert C#.net object into json using js

I'm trying to convert a C#.net object into json on the client side.
I'm not sure that this is even possible...
I know that the object has been serializable with BinaryFormatter class.
Is it possible ? or i'm wasting my time on it.

Working with identifiers as arrays and sending to MVC controller

I have some identifiers on my page. Named id[0], id[1] all the way to id[20] and sometimes more. I need to send these to an MVC controller and I would like to package them up as one object and unpack at the controller. Can someone tell me if this is possible, my knowledge of javascript is just basic so I'd really appreciate advice on which way to go. For example can I use JSON or serialize. btw I'm using jQuery.
Gordon
You can send your data in JSON.
See > ASP.NET MVC How to pass JSON object from View to Controller as Parameter
If you already have the id array (id[0], id1 , ... id[20]) you can convert it to JSON string with respect of JSON.stringify(id) where JSON.stringify function defined in the json2.js.
On the server side you can use for example Deserialize method of the JavaScriptSerializer to convert the data to List<T> where T is type of id[i] (for example string).
How to send data with respect of jQuery.ajax you already know from another answer.

How to Insert Javascript function between sql query?

Is there any way to insert java-script function between the query ??
For example :: In an query, i have stored date of birth like this 10101990 in the database, it means 10/10/1990, but I have display it in-front of page but in certain format like 10.10.1990 by using one java-script function.
Please suggest me any way ??
You should not be storing date as a string on the database. This might have a lot of issues. You have to store date as, well, a Date
You have to fetch this using a query using a language like Java or PHP.
When you get the date in the screen, you can format it in javascript, if required. However you can format it in PHP or Java. For java see SimpleDateformat.
See also this question.
You can manipulate with data values in jquery6 just before display it like this

ASP.NET MVC 2 Data Validation: Make C# Regex work for both C# and JavaScript simultaneously?

I have this C# regex:
^\s?((?<qty>\d+)\s?/)?\s?[$]?\s?(?<price>\d{0,2}(?:\.\d{1,2})?)\s?$
and use MVC's data validation at the client. JavaScript says this regex is invalid, although C# works perfectly fine. Any idea how to get it to work for both C# and JavaScript, since it doesn't seem possible to provide a separate JavaScript Regex in the Data Validation annotations?
The regex validates a quantity and price. 4/$2.69 for example.
Javascript does not support named reference (?<…>…). You need to use
^\s?((\d+)\s?/)?\s?[$]?\s?(\d{0,2}(?:\.\d{1,2})?)\s?$
and refer qty and price as 1 and 2 instead.
Remove the group names (<qty>).

Categories

Resources