C# to Javascript in MVC - asp.net-mvc-3-areas

In my MVC application i created the view Index.cshtml and there i declared the C# variable..
Eg: string selectedvalue="";
now how to use it in the javascript function code which i wrote with in this Index.cshtml, so that i can append some text to this string in javascript function?
string selectedvalue="";
// Coding Part
function onchangeFT(e)
{
'#selectedvalue'= e.value; //e.value come form combobox and it is should be assigned to "selectedvalue"
alert('#selectedvalue');
}
It is giving me error...
any idea to solve this issue?

I think you are confused about what you are doing in this case.
You have declared the C# variable #selectedValue using MVC razor syntax. You can then use this variable to inject it's value into the content that is output to the client. However, you cannot assign back to this variable from client side javscript code as it does not exist. Your error will be in relation to the fact that you are actually attempting to assign an object value back to a literal string declaration which is invalid.
In your example
'#selectedValue'=e.value;
would translate to
'' = e.value;
This is because MVC razor will automatically translate the c# param into it's value and output it literally. In order to assign a variable to the e.value you should create a javascript value as part of your script block which your script will then identify as usable target variable.
For instance:
var selectedValue = '';
selectedValue = e.value;

Related

Apex DateTime to String then pass the argument

I have two Visualforce pages, and for several reasons I've decided it is best to pass a datetime value between them as a string. However, after my implementation my date always appear to be null even though my code seems to compile.
I have researched quite a few formatting topics but unfortunately each variation of the format() on date time seems to not produce different results.
The controller on page 1 declares two public variables
public datetime qcdate;
public String fdt;
qcdate is generated from a SOQL query.
wo = [SELECT id, WorkOrderNumber, Quality_Control_Timestamp__c FROM WorkOrder WHERE id=:woid];
qcdate = wo.Quality_Control_Timestamp__c;
fdt is then generated from a method
fdt = getMyFormattedDate(qcdate);
which looks like this
public String getMyFormattedDate(datetime dt){
return dt.format(); }
fdt is then passed in the URL to my next VF page.
String url = '/apex/workordermaintenanceinvoice?tenlan='+tenlan +'&woid=' + woid + '&invnum=' + invnum + '&addr1=' + addr1 + 'fdt=' + fdt;
PageReference pr = new PageReference(url);
I expected when calling {!fdt} on my next page to get a proper string. But I do not.
UPDATE:
Sorry I guess I should not have assumed that it was taken for granted that the passed variable was called correctly. Once the variable is passed the following happens:
The new page controller creates the variable:
public String fdt
The variable is captured with a getparameters().
fdt = apexpages.currentPage().getparameters().get('fdt');
The getfdt() method is created
public String getfdt(){
return fdt;
}
Which is then called on the VF page
{!fdt}
This all of course still yields a 'blank' date which is the mystery I'm still trying to solve.
You passed it via URL, cool. But... so what? Page params don't get magically parsed into class variables on the target page. Like if you have a record-specific page and you know in the url there's /apex/SomePage?id=001.... - that doesn't automatically mean your VF page will have anything in {!id} or that your apex controller (if there's any) will have id class variable. The only "magic" thing you get in apex is the standard controller and hopefully it'll have something in sc.getId() but that's it.
In fact it'd be very stupid and dangerous. Imagine code like Integer integer = 5, that'd be fun to debug, in next line you type "integer" and what would that be, the type or variable? Having a variable named "id" would be bad idea too.
If you want to access the fdt URL param in target page in pure Visualforce, something like {!$CurrentPage.parameters.fdt} should work OK. If you need it in Apex - you'll have to parse it out of the URL. Similar thing, ApexPages.currentPage() and then call getParameters() on it.
(Similarly it's cleaner to set parameters that way too, not hand-crafting the URL and "&" signs manually. If you do it manual you theoretically should escape special characters... Let apex do it for you.

Loading a file from a parameter in Jasper

I'm new to writing jasper reports (and SQL in general). We are trying to load an RTF or HTML file as a disclosure at the end of a report. The way we are doing this is by selecting the name of the file ("Disclosure") in part of the SQL:
SELECT
....
'Disclosure' as Disclosure
FROM
...
And then, obviously, there is a field for this:
<field name="Disclosure" class="java.lang.String"/>
At the end, in the summary section of the report, we use the loadfile utility:
<textFieldExpression class="java.lang.String"><![CDATA[JasperFileRuntimeUtility.loadFile($F{Disclosure}, $P{REPORT_PARAMETERS_MAP})]]></textFieldExpression>
If the report returns data, this works beautifully. But if the result of the original query does not return any records, then the disclosure is not included in the report (since the result of the query is nothing, obviously).
I thought we could easily work around this by providing the "Disclosure" as a Parameter, but when I change that to $P instead of $F. I get an error about invalid io file type.
I also tried creating a Variable and setting that $V to the value of the $P we are passing in, but no luck there either.
Is there a load file type of utility that will load a parameter like we are doing with the field? Any other suggestions?
Appreciate the help!!!
I have understood you question better now so I edit the answer, you are calling
JasperFileRuntimeUtility.loadFile($F{Disclosure}, $P{REPORT_PARAMETERS_MAP})
you have no clue what function is this but you know that if you pass the String "Disclosure" it works.
The class JasperFileRuntimeUtility is within your library (its not an official jasper report function), try to search your project or your libraries.
It has a static method public static String loadFile(String value, Map<?,?> map)
Calling the metod with where $F{Disclosure} = "Disclosure"
JasperFileRuntimeUtility.loadFile($F{Disclosure}, $P{REPORT_PARAMETERS_MAP})
or
JasperFileRuntimeUtility.loadFile("Disclosure", $P{REPORT_PARAMETERS_MAP})
will not make any different the result will be the same (since the method have no other idea then with what parameters you call it).
Normally also calling with a $P{Disclosure} = "Disclosure"
JasperFileRuntimeUtility.loadFile($P{Disclosure}, $P{REPORT_PARAMETERS_MAP})
would be the same, but since the the parameter map is passed the function can see this parameter and maybe do something else...
More likely however since the parameter map is passed to function, you may have scriptlet or other calls that set static fields, and when you have no result the call to loadFile is not working since these static fields have not been set.
So if it is not working passing "Disclosure" this is most certainly the case..
Have fun!

zk MVC null check text box value data-bind annotation

My page has text-boxes whose value attribute annotated as #{bean.innerbean.prop}. Here if inner bean obj is null , it throws null exception since when it does "nullobj.prop" evaluation.
I'm using MVC 'not MVVM'. How to do null check on bean.innerbean then load or save value from bean.innerbean.prop.
'bean' obj I set it as page scope.it has other props as name,tooltip etc which obviously work well to other respective annotated components....
Please let me know is the way to do "empty", == null on #{……} ..
Im using annotation(in MVC long since in my app) to bind user entered-value to be saved on the object as well loading it on AnnotateDataBinder.loadAlll() method being invoke from AnnotateDataBinderInit class doAfterCompose(). My question anyway to do null check on the annotated bean value..... EL Expression evaluated once on the page load phase thereafter it wont work
First of all, if you are using MVC you use the wrong annotation.
${} is used for el expressions, while #{} is used for data binding, witch is MVVM.
So if you use the correct expression you can do :
${empty bean.innerbean?'empty':bean.innerbean.prop}

FindBy property in TYPO3 Extbase MVC is not working

I'm unable to run the FindBy magic function property in Extbase MVC
$title=array(0 =>'Books Day');
$each_event=$this->eventRepository->findByTitle($title);
$each_event is returning an object of type TYPO3\CMS\Extbase\Persistence\Generic\QueryResult .
How do I make this work?
I also tried passing in a string to findByTitle and findByOne. Both don;t work! :(
I'm using TYPO3 6.1 and extension builder.
The last part of those magic functions always needs to be a field in the database. So "title" must be in your model. You might have a field "one" for your object, but I guess you meant findOneByTitle?
The object type QueryResult is correct. You can turn it into an array for debugging purpose for example:
$foo = $query->execute()->toArray();
By the way: check wether your eventRepository is null or not and you could try this to see if it works at all:
$result = $this->myRepository->findAll();
Try
$each_event=$this->eventRepository->findByTitle($title)->toArray();
Reference to the QueryResult.
As said in the documentation, it returns a QueryResultInterface|array.
As a consequence you have to loop over the result like this:
foreach($each_event as $single_event) {
$single_event->getProperty();
}
If you are sure that it returns only one single value you could also access it by the index 0:
$each_event[0]->getProperty();

umbraco.library.NiceUrl() returns error when passing a content node id

I have created a doctype in umbraco which has one of the following property:
Property - Case study link
Datatype - Content picker
I need to fetch the URL of this document in a Razor macro and assign it to a hyperlink.
Currently am doing it in this way but it's giving me an error:
#foreach (var item in #Model.OurWork){
Read case study
}
And here is the error I get on viewing the page:
Error loading Razor Script OurWorkGrid.cshtml The best overloaded
method match for 'umbraco.library.NiceUrl(int)' has some invalid
arguments
I have tried outputting the node id without using the niceURL() function and it works fine (outputs 1088).
Read case study
results in this:
Read case study
But as soon as I put back NiceURL() function, it chokes again.
I really don't know what am I doing wrong here!
Instead of using the umbraco library method, try loading the node with the ID first, and then using the Url property to get the nice URL.
#foreach (var item in #Model.OurWork){
var caseStudyNode = #Model.NodeById(item.caseStudyLink);
Read case study
}
Also, add some form of a check to make sure the value is set, in case it's not a mandatory property on the doc type. Here's one example:
if (#item.HasProperty("caseStudyLink") && !string.IsNullOrEmpty(#item.caseStudyLink))
{
...
}
Try something like:
#foreach (var item in #Model.OurWork){
Read case study
}
You may want to check first whether item.caseStudyLink contains a value because this will throw an error otherwise.