Grouping identical labels and changing text all together - c# / Javascript - c#

I have a asp.net C# MVC Razor view that allows the user to change units for input fields from metric to imperial. There are about a dozen labels that all need to be changed at the same time to the same text, no exceptions ("mm" => "inch" and visa verse).
Since the dot net framework requires each element to have unique ID fields I'm trying to find another way to group them all together and change them on the client side.
It seems silly to give each one a unique ID and call each one individually in a if/then statement to switch measurement systems when they are identical. There has to be a better way.
I attempted to use #ViewBag but I found that javascript can only read the value and can't change it on the client side.
#Html.Label("display_units", "mm", new { id = "lbl_units" })
Thanks for your help and suggestions.

Give your labels a common css class like "display-units". Then from Javascript code, you can use jQuery to find all labels of that class and change the text:
$(".display-units").text('mm');

KnockoutJS was written to solve this kind of problem, ie, binding your viewmodel to your view.

Related

Umbraco multilanguage with URL change

I am working with Umbraco v7.x. I have few static pages and they need to be added in two languages(en/da).
I know there are two ways to translate
1- Copy folder and assign different culture and hostname and add fields data according to language.
2 - Use dictionary items.
But my problem is customer wants to have custom fields on all pages so he can change static page data without having the need to ask developer. So if I use first method to change language that would also change URL which is not required for this solution.
Second I use dictionary than how can customer can change field data because he had to go to dictionary items and make any change there. This is not a problem but text needs to be formatted and this is not possible if I use dictionary items.
Any work around to this problem.
Thanks
I recommend using Vorto if you want a 1:1 translated site (meaning each piece of content has a translation for each language. Use dictionary items for text that was hard-coded into your template but Vorto will wrap your property editors so that you can edit each language in the same node. You can then use HasVortoValue() and GetVortoValue() instead of HasValue() and GetPropertyValue() methods that come with Umbraco. This will return the correct value based on the culture of the request. You will also need to configure Umbraco to load the multilingual content by setting a host name and associate that with a culture. You do that by selecting "Culture and Hostnames" in the contextual menu for the home node and and click "Add Domain" (you will need to have first added the language in the Settings section):
Alternatively, if you want to use a subfolder for each language instead of a differeent domain (e.g. sitename.com/english instead of english.sitename.com) you can create a custom Content Finder. I have written a couple blog posts on how to do that here and here.

Knockout-style dynamic page with Razor viewmodels and #functions?

I'm building a page in which the user can query a set of data by building up a set of search criteria, something like the way the Visual Studio TFS plugin lets you search work items: a table of conditions, where you can keep adding rows. You select "and" or "or" for the join condition, then select a field, enter a value, and select whether you want things that do or do not match it:
1. show items where [Field] [is|is not] [value]
2. [and|or] [Field] [is|is not] [value]
3. [and|or] [Field] [is|is not] [value]
etc...
Now, I'm looking at ways to build this, and I had a thought. In the past, I've used Knockout, but this requires me to have models in Javascript to map the data to, which seems redundant when I already have those models in C# in the server-side code. Of course, I can use Razor code to foreach through a list of criteria that's part of the model in a strongly-typed view, but I can't find a tidy way to add to this list.
The model structure in C# is (roughly) like this:
Field:
field name
list of options for the value
boolean value for the is/is not option.
Criterion:
Field
selected value
combination type (and/or)
Query:
list of Criterions (that looks weird not saying Criteria)
start and end date
the user's access level
view field and sorting options
QueryViewModel:
Query
assorted lists to populate the view options selection area
a little metadata for other (unrelated) displays on the page
In Knockout, I'd add an on-click method to the "add search criteria" button to add new entries to the list of criteria. Can I use a Razor functions block (#functions { ... }) to achieve a similar result? I've tried a few things, but I either find that the viewmodel doesn't seem to be in scope, or that there's no way to update the page to show the new contents of the viewmodel (although I'm experimenting with something that involves passing the newly-updated viewmodel to a partial view, which might work somehow). Can this be done, or do I need to take a deep breath and go back to the Javascript?
You can serialize/deserialize your C# objects as JSON, either with standard MVC controllers and JSONResult or with Web API in MVC 4. This means you dont have to explicitly redefine your c# objects in javascript.
In the browser you can use the knockout mapping plugin to make your json properties into knockout observables if required.
This is generally a cleaner and more robust approach than dynamically loading and rendering html from the server (if thats what you were suggestiong - wasnt 100% clear from your post).

adding razor statement in javascript

Im trying to add some C# code in my javascript. But I can't parse my var name into the C# code.
What I want is this:
$.ready(function(){
var name = $("#input");
#Class.text(name)
});
But it wont allow me to parse the name.
This is a short version of what i got, the #Class represent a more complex function
The syntax is indeed incorrect.
The var name = $("#input"); is javascript. It is executed at the client end after the document is loaded. The #Class is server side and is executed when the page is being generated.
You cannot use javascript variables in C#. What is it that you are trying to achieve; there may be a different way to do so.
--
Update : You are trying to call a c# method from javascript.
Overall this will require a post back to the server - see http://blog.bobcravens.com/2009/11/ajax-calls-to-asp-net-mvc-action-methods-using-jquery/
#MichaelTotKorsgaard Based on our discussion in the comments of your question, I think I now understand. Unfortunately what you're trying to do simply won't work. It doesn't work that way. The reason you do AJAX/JSON is because, once C#/MVC/ASP.NET returns HTML (it doesn't actually know if what it returns is valid), it's up to the Client to "do stuff" with it. That's where Javascript comes in. But the server-side MVC and client-side JS don't actually know anything about each other. You Need to either 1) post back or 2) use AJAX.
It's time to saddle up and learn how to do it :)
If you give more details about what you're actually trying to accomplish -- like what you're trying to load from JS/AJAX -- then I'm sure SO can give you more information to set you on the right path.

HTML tags in database bad practice or good practice?

Sometimes I need to format specific data or part of it that comes from the database .
For example :
If i have a desc (stored in DB) like this :
HTML 4 has been tweaked, stretched and augmented beyond its initial scope to bring high levels of interactivity and multimedia to Web sites. Plugins like Flash, Silverlight and Java have added media integration to the Web, but not without some cost.
and i wanna to format the last line , change the font and color for example .
What 's the best practice to do this ?
embedding HTML tags in my database ??Is this safe and the best practice or there 's some way to separate the structure layer from the presentation layer from the behavior layer ?
If you plan to manipulate or search upon the stored data then do not store HTML markup in your database. Imagine that at some point you're asked to change the fonts from Tahoma to Georgia, change <b> tags to <strong> or allow the users to search on the HTML column; and searches for strong end up returning irrelevant information because strong is also a frequently used HTML tag.
Storing HTML markup in your database is also a bad idea if you do not check what is being stored. A malicious script tag such as <script>location = 'http://otherwebsite'</script> is just one naive example.
Ideally you should store the data as-is or use some kind of markup such as (wiki or markdown) to store basic formatting information.
There IS some way to separate the data from the presentation. You keep them separate! If you want to do some formatting on that text that you pulled from the database, go ahead and do that in your application code. Note that structural markup is an entirely different topic from presentation markup (font, color, layout, etc)
http://en.wikipedia.org/wiki/Separation_of_presentation_and_content talks about this very point and makes a clear separation between presentation markup and structural markup in the paragraph under Intended Meaning.
Storing formatting tags in your data generally points to poor separation between the two layers or a data model that isn't sufficient to represent your data properly. As the author is storing data in a database, that might indicate that he has just a single field for holding the "content block" of an article rather than multiple fields for the author, title, body, references, etc. For user input data, we often fall back to a markup inside the user content for designating structure. That happens through "fake" html tags or even real html/xml tags like <h1>, <em>, <a>, etc.
Note that I'm not objecting to structural markup on principle but I would look carefully at why it's required if you're storing it in a database. I am objecting to presentation markup on principle.
It depends on where the data comes in to DB.
If you're the only one who changes the DB content, then it is perfectly normal to store HTML tags in it.
Otherwise, if you store your users input in DB, there are two approaches:
1) To sanitize the input supplied by your users (either on store or on display) to make sure no malicious data will be displayed.
2) To use some intermediate markup language with the limited possibilities (such as BBCode), and to compile it to HTML (again, either on store or on display).
I can not recommend to store any html tags inside your database. In the end you will find yourself lost if your codebase gets bigger and as well if you want to change your html. For example add some arguments to your html tags like classes or similar. You would need to "fix" all the html tags with sql statements. This also counts for the case you want to do something else with your data. For example create RSS Feeds or export it to another format like for example an excel sheet or similar.
Why do you want to do it anyway? I am sure tere is a better solution to your problem.
Try to separate the content form the application layer. Normelize your data and put paragraphs for example in a new dataset. If you really need to for example color one word, I would follow the suggestion that has already been posted. Use some own syntax like [color-a][/color-a]. The export problem could however been solved by striptags()
You can use blob field, however you won't be able to do full searching on it iirc. If you have a column with the template name as a value and a blob with the html template value then this will work out just fine.
IMO it's perfectly fine to store HTML in your database. You sound smart enough to not allow just anything to go into the DB without validation.
You just need to be careful about how it's updated. If you are inserting to the database via code:
INSERT INTO myTable Values(x + y + z)
if the variable x has some HTML in it with single quotes for example, no bueno.
I think the content of the string you stored in database has nothing to do with the presentation layer, its only affection is how your business layer provide the html string (directly read from database or decorate it later) to the presentation layer.

in jqgrid, is there a way to programatically set multi search filter criteria (from the server side of an asp.net-mvc app)

i want to automate a user going into the "Find Records" / multi search UI and preset a filter in jqgrid to
Specific field contains "abc"
Second field does not equal "123"
is this possible in jqgrid? i can set the toolbar filter by just adding item to the query string (Field1="test") so, in my asp.net-mvc controller action, i would do something like this:
string name = "Joe";
return Redirect("/Project?Owner=" + name);
but i now want to replicate the support for the advanced search so i can do
Multiple Fields
Different operator (equals, does not equal)
i would like it to work so if the user did click on the Filter button that it would be prepopualated with these filter just like as if they would have done this initial filter manually like this:
I see this question but i want to be able to do this from the server side. Is there anyway to set postdata from the serverside of any asp.net mvc app??
Presetting of the filter is nothing more as setting pf postData jqGrid parameter. See the old demo (see the answer). If one set search:true the filter will apply (see here and here).
Depend on how you organize you pages it can be very simple to preset the filter property of the postData. You can for example include on the corresponding server generated page the inline <script> which define a global variable with the filter and use it in the grid definition. The filter you can set user depended.
I suggested Tony (see here) to include more support for predefined filters. In my vision if would be nice to predefine some probably complex filters and allow the user choose the filter by name. The way seems me especially good for the corporate clients.

Categories