This topic might be opinionated. If it is so, apology. I need to assess the below questions:
Should I use ASP.NET MVC controller to generate chart and return the image to the client side?
If this approach is chosen, is there any reliable free MVC chart control that I could use?
Should I use Client Side charting control to generate chart and return the JSON data to be consumed by the client side?
If I go along using this approach, how to secure the data consume by the client side? I want to prevent the plain data is being copied over or read by simply view source feature of the browser.
Thanks!
Disclaimer: I didn't try this in ASP.NET MVC > 3, but probably works fine.
As a follow-up for my comments. If you need just: Bar Chart, Pie Chart, Column Chart, you can use ASP.NET Web Helpers Library https://www.nuget.org/packages/Microsoft.AspNet.WebHelpers/.
In your controller you'll have an action 'DrawChart' or something like this (for PNG format) :
public ActionResult DrawChart()
{
var chart = new Chart(width: 300, height: 200)
.AddSeries(
chartType: "bar",
xValue: new[] { "10 Records", "20 Records", "30 Records", "40 Records" },
yValues: new[] { "50", "60", "78", "80" })
.GetBytes("png");
return File(chart, "image/bytes");
}
And view : <img src="#Url.Action("DrawChart")" alt="Drawing chart with HTML Helper" />
An example here : http://weblogs.asp.net/jalpeshpvadgama/chart-helpers-in-asp-net-mvc3
Give a look at this library.
It uses a Canvas control (instead of PNG), giving you interactivity and animation.
The data are generate server-side (so you don't need to expose your datasource) and pass it to chartJS, an open source library that draws the graph.
Related
I have a power bi report which i am embedding on my web application.
Is there is anyway that is can embed directly a drill down view of the report?
in short i want to open the report in drill down mode on.How can i do this programmatically.
I am using dot net mvc for my web application.
Yes, that is possible (assuming that you want to apply a "default" filter in your report). When you embed the report, you can add a list of filters to your embedConfig (documentation):
var embedConfig = {
...
filters: [...]
};
A single filter, in the list of filters, can look like the following (documentation):
const myFilter = {
$schema: "http://powerbi.com/product/schema#basic",
target: {
table: "Table1",
column: "Column1"
},
operator: "In",
values: ["A"]
};
In which case you specify the filters in embedConfig according to
filters: [myFilter]
However, note that this is not a secure way to prevent users from seeing data, a user can easily, using javascript, remove the filter and gain access to all the data.
I'am new in MVC. I'am currently working to transform a desktop to a web application.
I need to make an update to the user view when an event of an object occurred. I have an object that observe a humidity sensor. Let say This object will trigger an event when the humidity above 70%. The code might be like this:
Private Sub Humidity_Alert(sender As Object) Handles objSensor.Alert
'Update user chart
End Sub
In desktop application, I just make an update to the view as usual in realtime, but I don't have any idea yet how to return this event to the client in MVC, without using javascript timer to make an ajax call to request if there is any alert. How to solve my problem?
I would suggest using ASP.NET SignalR library: signalr.net
You can use it for real-time updates from server to client.
ASP.NET SignalR is a new library for ASP.NET developers that makes it incredibly simple to add real-time web functionality to your applications. What is "real-time web" functionality? It's the ability to have your server-side code push content to the connected clients as it happens, in real-time.
Some pseudo-code example:
SignalR Hub:
public class HumidityHub : Hub
{
public void RefreshChart(string data)
{
Clients.All.refreshChart(data);
}
}
ClientCode:
var hub = $.connection.humidityHub;
hub.client.refreshChart= function (data) {
//refresh your chart
};
$.connection.hub.start();
ServerCode:
var hubContext = GlobalHost.ConnectionManager.GetHubContext<HumidityHub >();
hubContext.Clients.All.refreshChart(data);
I am porting a web forms application to mvc. The forms app made heavy use of the ReportViewer control which does not extend well in the mvc world. I have started from scratch. The only properties I can not reliably replicate without a custom renderer is Page Count and Total Page Count. How to get these value using the ReportExecution? I am really hesitant to invest time/resources into a custom renderer. The only thing I can think of and do not like is setting the DeviceInfo.Section and catch the exception when it is not in range. The report is being rendered in HTML40 format.
Continue to use the ReportViewer control embedded in an .aspx page, and create an MVC route to this page:
routes.MapPageRoute(
"Reports",
"Reports/{folder}/{name}",
"~/Reports/default.aspx",
false,
new RouteValueDictionary(
new { folder = "", name = "", controller = "", action = "" }),
new RouteValueDictionary(
new { constraint = new ReportConstraint() })
);
I'm still quite new to ASP.NET MVC and wonder how-to achieve the following:
On a normal view as part of my master page, I create a varying number of partial views with a loop, each representing an item the user should be able to vote for. After clicking the vote-button, the rating shall be submitted to the database and afterwards, the particular partial view which the user clicked shall be replaced by the same view, with some visual properties changed. What is the best practice to achieve this?
Here's how I started:
1. I defined the partial view with an if-sentence, distinguishing between the visual appearance, depending on a flag in the particular viewmodel. Hence, if the flag is positive, voting controls are displayed, if it's negative, they're not.
I assigned a Url.Action(..) to the voting buttons which trigger a controller method. In this method, the new rating is added to the database.
In the controller method, I return the PartialView with the updated ViewModel. UNFORTUNATELY, the whole view get's replaced, not only the partial view.
Any suggestions how-to solve this particular problem or how-to achieve the whole thing would be highly appreciated.
Thanks very much,
Chris
Trivial (but by all means correct and usable) solution to your problem is Ajax.BeginForm() helper for voting. This way you change your voting to ajax calls, and you can easily specify, that the result returned by this call (from your voting action, which will return partial view with only 1 changed item) will be used to replace old content (for example one particular div containing old item before voting).
Update - 11/30/2016
For example:
#using (Ajax.BeginForm("SomeAction", "SomeController", new { someRouteParam = Model.Foo }, new AjaxOptions { UpdateTargetId = "SomeHtmlElementId", HttpMethod = "Post" }))
ASP.NET MVC is a perfect framework for this kind of needs. What I would do if I were in your possition is to work with JQuery Ajax API.
Following blog post should give you a hint on what you can do with PartialViews, JQuery and Ajax calls to the server :
http://www.tugberkugurlu.com/archive/working-with-jquery-ajax-api-on-asp-net-mvc-3-0-power-of-json-jquery-and-asp-net-mvc-partial-views
UPDATE
It has been asked to put a brief intro so here it is.
The following code is your action method :
[HttpPost]
public ActionResult toogleIsDone(int itemId) {
//Getting the item according to itemId param
var model = _entities.ToDoTBs.FirstOrDefault(x => x.ToDoItemID == itemId);
//toggling the IsDone property
model.IsDone = !model.IsDone;
//Making the change on the db and saving
ObjectStateEntry osmEntry = _entities.ObjectStateManager.GetObjectStateEntry(model);
osmEntry.ChangeState(EntityState.Modified);
_entities.SaveChanges();
var updatedModel = _entities.ToDoTBs;
//returning the new template as json result
return Json(new { data = this.RenderPartialViewToString("_ToDoDBListPartial", updatedModel) });
}
RenderPartialViewToString is an extension method for controller. You
need to use Nuget here to bring down a very small package called
TugberkUg.MVC which will have a Controller extension for us to convert
partial views to string inside the controller.
Then here is a brief info on how you can call it with JQuery :
var itemId = element.attr("data-tododb-itemid");
var d = "itemId=" + itemId;
var actionURL = '#Url.Action("toogleIsDone", "ToDo")';
$("#ajax-progress-dialog").dialog("open");
$.ajax({
type: "POST",
url: actionURL,
data: d,
success: function (r) {
$("#to-do-db-list-container").html(r.data);
},
complete: function () {
$("#ajax-progress-dialog").dialog("close");
$(".isDone").bind("click", function (event) {
toggleIsDone(event, $(this));
});
},
error: function (req, status, error) {
//do what you need to do here if an error occurs
$("#ajax-progress-dialog").dialog("close");
}
});
There needs to be some extra steps to be taken. So look at the blog post which has the complete walkthrough.
In ASP.NET MVC, we don't have controls which can do stuffs such as autocompletion (we don't have controls at all from Visual Studio like in web forms).
But, Is it possible to still get 'Auto-completion' with a text box in ASP.NET MVC (like with the textbox found on the Google's Home page)?
If this is the case, what's the best way to do so? JQueryUI? or there's someother way of getting that functionality?
Thanks for helping.
You mean like the one in the jquery ui?
http://jqueryui.com/demos/autocomplete/
To use JQuery Autocomplete with ASP.NET MVC you can do the following:
Step 1 : Create the textbox
<%= Html.TextBox("searchBox",null,new { #class="large", autocomplete="on"}) %>
Step 2 : Create action to do search function (I am using NHibernate to query DB, but the key is to return an string with each result delimited by a newline)
public ActionResult Autocomplete(string q) {
var programs = programRepository
.Search(q)
.ToList()
.ConvertAll(x => x.ProgramName)
.ToArray();
return new ContentResult {Content = string.Join("\n", programs) };
}
Step 3: turn on autocomplete functionality in your HTML (place inside a script block)
$(document).ready(function() {
$('#searchBox').autocomplete("/Controller/Autocomplete", { autoFill: true, minChars: 2 });
});