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

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).

Related

How to display a search result in a treeview

Currently, I'm working in a MVC project (this is my first project). I'm doing fine but I'm stuck somewhere. I hope someone will help me out with this.
In this project I have to search for a record (with id) in SQL database from visual studio, where I should get result in a treeview... like under ID we may have a lot of sub ids or may not, if we have one sub id, it should display one if we have multiple sub ids multi-level treeview should display.
Note: This result I should get from the database when the user searched for particular id only, DATA from the database should not be loaded with the page.
Create a tree view using css and html. Populate the value using a rest controller or controller.
create an arraylist which returns your search result.
return the value from the list to a model or a url using a function.
As you can see there are many ways you can do this.
you can check this out as well: https://www.c-sharpcorner.com/UploadFile/85ed7a/searching-records-from-database-and-display-in-gridview-and/
and for only loading id from the database you can run
Select id From tablename
and if you want the details only after user clicks the id
Select * From tablename Where id = clickedvalue
you could also use where statement in sql to have more specific values
you'll need to change few codes as this tutorial uses grid view.
i hope this helps. But please provide some code snippets of what you've done or where you're stuck to get a precise answer.

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.

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

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.

Getting more than one item from a database in ASP .NET MVC 3

I'm creating a database where users can enter some Error Reports and we can view them. I'm making these database with C# in the ASP MVC 3 .NET framework (as the tags imply). Each Error Report has a unique ID, dubbed ReportId, thus none of them are stored under the same Id. However, whenever a User creates a new Error, I pass their User Name and store it in with the rest of the report (I use User.Identity.Name.ToString() to get their name and store it as a string). I know how to get a single item from the data using a lambda expression, like so:
db.DBSetName.Single(g => g.Name == genre)
The above code is based on an MVC 3 tutorial (The Movie Store one) provided by ASP. This was how they taught me how to do it.
My major question is: is there a member function like the .Single one that will parse through the whole database and only output database entries whose stored User Name matches that of the currently logged in user's? Then, I can use this to restrict User's to being only able to edit their own entries, since only their entries would be passed to the User's View.
What would be the best way to implement this? Since the ReportId will not be changed, a new data structure can be created to store the user's Errors and passed through to the Index (or Home) View of that particular controller. From there they should be able to click any edit link, which will pass the stored ReportId back to the Edit Action of this particular controller, which can then search the entire database for it. Am I right in assuming this would work? And would this be ideal, given that the other items in the database are NOT passed through to the Index in this method, meaning the User does not have access to the other items' ReportId's, which the user needs to pass into the Edit Action for it to work? If this is ideal, this is the method that requires me to know how to parse through a database and grab every element that fits a particular description (stored User Name matches User's current User Name).
Or would a better approach be to pass the whole database to the Index View and only output the database entries that have User Name values that match the current logged in user's? I guess this could be done in a foreach loop with a nested if loop, like so:
#foreach(var item in db.Reports)
{
if(item.UserName == User.Identity.Name.ToString())
{
...code to output table...
}
}
But this passes the whole database which gives the user a lot more info than they need. It also gives them potential access to info I don't want them to have. However, I don't have to make a new data structure or database, which should lower server memory usage and fetch time, right? Or are databases passed by copy? If so, this method seems kinda dumb. However, I don't know if the first method would fracture the database potentially, this one certainly would not. Also don't remember if I NEED an else statement in C#, I'm more familiar with C++, where you don't need one and you also don't need {}'s for single line if's, if I need one: please don't judge me too harshly on it!
Small note: I am using CRUD Controllers made with the Entity First Framework in order to edit my database. As such, all creation, reading, updating, and deletion code has been provided for me. I have chosen not to add such basic, common code. If it is needed, I can add it. I will add what the Edit Action looks like:
public ActionResult Edit(string id)
{
Report report = db.Reports.Find(id);
return View(report);
}
It accepts a string as an id, ReportId is the id used and it IS a string. It is a randomly generated GUID string made with the GUID.NewGuid().ToString() function. I will also be doing the comparison of names with:
Model.UserName == User.Identity.Name.ToString()
Which was shown earlier. Sorry if this is too much text, I wanted to provide as much info as possible and not make anyone mad. If more info is needed, it can certainly be provided. So at the end of the post, the major question actually comes down to: which of the above two methods is best? And, if it's the first one, how do I implement something like that?
Thanks for your help!
Unless I'm completely misunderstanding you, you just want .Where()
Like this:
var reports = db.Reports.Where(r => r.genre == inputGenre);
This would get you an IEnumerable of Report, which you could then use however you wish.

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