c# JSON date issue - c#

I have a date in my DB of 2014-03-03 05:00:00, which is being rendered in JSON as:
the date is /Date{(-6xxxxx)/ and I call this method to parse it:
function parseJsonDate(dateString) {
var result = new Date(+dateString.replace(/\/Date\((-?\d+)\)\//gi, "$1"));
var result = new Date(parseInt(dateString.replace('/Date(', '')));
result.format("dd-MM-yyyy");
return result;
}
when running, i comment out one of the results lines, but get the same result for both:
the method is being called from Jquery template like such:
<tr>
<td>
<span id="approvedDate"><i class="glyphicon glyphicon-time" data-toggle="tooltip"
data-original-title="approved date"></i> ${parseJsonDate(AuditDate)}</span>
</td>
</tr>
EDIT
What a muppet.. I spent so long thinking this was a JSON vconversion issue, i totally forgot to go back and check my dapper code. my ApprovalHistory objec had AuditDate, but I was asking for EnteredDate in the sql. So, it was doing as expected.
aaaaaaaarrr :-)

I am seeing something fishy there
var result = new Date(+dateString.replace(/\/Date\((-?\d+)\)\//gi, "$1"));
var result = new Date(parseInt(dateString.replace('/Date(', '')));
you are making two variables named result within the same closure, is it intentional?
what does result.format do? since result is a Date object I wouldn't assume that it would change the original type from Date to string.
Maybe
var s = result.format("dd-MM-yyyy");
return s;
is what you really want to do?
you can do this after the ajax complete, this will save you tons of trouble having to parse the Date(xxxxx) thing over and over again
data = data.replace(/\"\\\/Date\((-?\d+)\)\\\/\"/g, '$1')
this will convert "Date(xxxx)" to xxxx and then you can just call new Date(xxxx) to make new Date object.

Maybe you could use something like this:
var str = (result.getMonth() + 1) + "-" + result.getDate() + "-" + result.getFullYear();
return str;

Related

C# get parameter from url after redirect

I have a redirect:
string linkedInLogin = "https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=ID&redirect_uri=https%3A%2F%2Fwww.example.com%2Fauth%2Flinkedin&state=ZEKNI&scope=r_basicprofile";
Response.Redirect(linkedInLogin);
This results in a redirect in my browser to
https://www.example.com/auth/linkedin?code=CODE_I_NEED&state=STATE
How do I retrieve the value of the code paramater?
Request.Querystring["code"] gives me a null value. The problem is that the browser "thinks" the current URL was /home/MyController.
try:
HttpContext.Current.Request.Params["code"]
this way you can get a combined collection of QueryString, Form, Cookies, and ServerVariables items.
here you can read more: HttpRequest.Params Property
Do you have that link stored somewhere or are you asking for how to retrieve it as well?
Assuming you already have it:
var s = "https://www.example.com/auth/linkedin?
code=CODE_I_NEED&state=ROB962242SMUT"
You can either do:
string s = "https://www.example.com/auth/linkedin?code=CODE_I_NEED&state=ROB962242SMUT";
var code = s.Substring((s.IndexOf("code=") + 5), (s.IndexOf('&') - (s.IndexOf("code=") + 5)));
//CODE_I_NEED
Or
String sourcestring = "https://www.example.com/auth/linkedin?
code=CODE_I_NEED&state=ROB962242SMUT";
Regex re = new Regex(#"code=(.*)&");
var result =Match m = re.Match(sourcestring).Value;
//CODE_I_NEED
edit: based on the other two answers i may not have understood your question properly xd

How do I convert the result from the lambda expression to a string in Razor c#?

First of all, please forgive my ignorance, I am completely new to C# and the MVC format.
I am attempting to pass the resulting value from an item in my model into a custom function. I want to pass a string so I can manipulate it and then pass a string back to the View so I can update my CSS code. The basic form is:
#foreach (var item in Model)
{
string tabval = "";
string status = "";
//This is where I am getting confused, how do I pass the value of
modelItem => item.status to my function StatusType()?
tabval = StatusType(status);
<td class="#tabval.ToString()">
#Html.DisplayFor(modelItem => item.status)
</td>
}
Thank you for any help or guidance that you can give. If there is a better way to handle this type of action, I'd be happy to hear those suggestions as well.
Just replace
tabval = StatusType(status);
with
tabval = StatusType(item.status);

Razor syntax - using two variables in string

SomeObject record = new SomeObject();
record.value1 = 1;
record.value2 = "hello";
<td><input type="checkbox" id="indicator_#record.value1_#record.value2" /><td>
What is the proper razor syntax to create a checkbox with an id of "indicator_1_hello"?
When attempting this way, it says the object doesn't contain a definition for value1_ (understandable) and when I tried "indicator_#record.value1#_#record.value2" if had a runtime error about something named _ not existing in the context (again, understandable).
edit:
As a temporary solution I've done:
SomeObject record = new SomeObject();
record.value1 = 1;
record.value2 = "hello";
var combined = String.Format("{0}_{1}", record.value1, record.value2);
<td><input type="checkbox" id="indicator_#combined" /><td>
I am still curious if you can do it all inline though.
#{
// just for testing
var record = new { value1 = "foo", value2 = "bar" };
}
<input type="checkbox" id="indicator_#( record.value1 + "_" + record.value2 )">
Gives: <input type="checkbox" id="indicator_foo_bar">
Just make sure that you aren't building an ID which would be better auto-generated by the natural hierarchy of your view model. Most of the time, you shouldn't need to manually construct IDs in your view.
If you need something like this, I'd suggest adding that field (i.e. CheckboxID) to your model and populate it on the server side, before passing it to the view.

how to remove empty line use regex in javascript and c#

user input content by text editor, and finally submitted to the database.
before store in database,i want remove empty line in content at begin and end (the middle can not be removed).
i want use JavaScript and C#
sample content is:
<div>
<p><span><br></span></p>
<span>a<br/>bc</span>
<p>te<br>st</p>
<p>\n<span>\n</span></p>
<p><span><br/></span></p>
</div>
i need is:
<div>
<span>a<br/>bc</span>
<p>te<br>st</p>
</div>
who can help me?
Well if I understand what you are trying to accomplish, this should solve your problem:
string input = #"
<div>
<p><span><br></span></p>
<span>a<br/>bc</span>
<p>te<br>st</p>
<p>\n<span>\n</span></p>
<p><span><br/></span></p>
</div>
";
string pattern = #"(<p>)?(\\n|<br/?>)?<span>(<br/?>|\\n)</span>(</p>)?";
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(pattern);
string final = reg.Replace(input, String.Empty);
Console.WriteLine(final);
}
That above code will return:
<div>
<span>a<br/>bc</span>
<p>te<br>st</p>
</div>
You could then go about trimming ever line, as it looks like it needs it.
It is not mentioned in the question whether you want to clean up your content on the client or server side.
If it should be done on the server please don't use regex for it. Why? See this excellent answer. Use HTML parser instead. E.g. with HtmlAgiltyPack:
var doc = new HtmlDocument();
doc.LoadHtml(html);
foreach(var node in doc.DocumentNode.SelectNodes("//div|//span|//p"))
if (string.IsNullOrWhiteSpace(node.InnerText.Replace(#"\n", string.Empty)))
node.Remove();
var result = doc.DocumentNode.OuterHtml;
But it could be done even simplier on the client (without regex too) by using jQuery:
var dom = $(html);
dom.find('p,span,div').each(function() {
if ($(this).text().trim() == '')
$(this).remove();
});
var result = dom.wrap('<div>').parent().html();

How to get text from LINQ2SQL query?

I have a web page in which I am giving USER the options of writing notes. Now when ever the web page checks that a USER is:abc then it pulls up the note from the MEMO Table.
Here is my code in Page_Load():
using (EntityMemoDataContext em = new EntityMemoDataContext())
{
int getEntity = Int16.Parse(Session["EntityIdSelected"].ToString());
var showMemo = from r in em.EntityMemoVs_1s
where r.EntityID == getEntity
select r.Memo;
tbShowNote.Text = String.Join(#"<br />", showMemo);
}
tbShowNote is showing me value like this:
test<br />test1<br />test1<br />test4<br />test4
And I want it like this:
Test
Test1
Test2 ...
tbShowNote is a TextBox!
You only asked for the first memo, so that's what you got back. If you want it enumerated with each one on it's own line in html, you could do this:
using (EntityMemoDataContext em = new EntityMemoDataContext())
{
int getEntity1 = Int16.Parse(Session["EntityIdSelected"].ToString());
var showMemo = from r in em.EntityMemoVs_1s
where r.EntityID == getEntity1
select new
{
r.Memo
};
tbShowNote.Text = String.Join(#"<br />", showMemo);
}
The key takeaway is if r.Memo is of type string, then the LINQ query you executed gave you back a IQueryable<string>. It's on you to decide if you want to flatten that list later.
Edit: Equiso made a good observation in that you're actually returning an IQueryable of an anonymous type, not IQueryable<string> due to the new { ... } syntax. I'd say combine his answer with mine and run with it:
var showMemo = from r in em.EntityMemoVs_1s
where r.EntityID == getEntity1
select r.Memo;
tbShowNote.Text = String.Join(#"<br />", showMemo);
The problem is in the select part of your linq query, you are wrapping your results in an anonymous type, that is why when you call ToString() you see { Memo = test }. You probably want it like this:
var showMemo = from r in em.EntityMemoVs_1s
where r.EntityID == getEntity1
select r.Memo;
After that showMemo will contain just strings.
It looks like your showMemo is a collection and you are then just assigning the top value? If you are putting them in one string then you need to aggregate them together.

Categories

Resources