Dealing with XML in ASP.NET MVC - asp.net

I have a block of XML in a database which is easy enough to pull out using ASP.NET MVC, however I would like to access and modify the XML in an way more consistent with class instances. Is there a way to get the MVC (or any other model) to generate a data access (or perhaps Entity) class set from the DB-stored XML?
If the above is rather obtuse, the question could be summarised as; What method would you use to best access and modify XML stored in a database from an ASP.NET MVC application?

The method I went with in the end was simply to use LINQ2SQL to get the data from the SQL2005 DB and then pass the field value into XElement.Parse() to get an XML object I can easily work with. Manipulating the XML was the done using the information gained from helpful overflowers here:
How do I insert an element into XML using Linq?
How to add attributes to an element using LINQ, C#?
How to sort XML in LINQ C# by an attribute value? Also MVC

Related

How to put ASP.NET Json WebHelper object into a database

I want to take the data from my json file, parse and send to my database. Each field in my json objects have to go to it's corresponding column in my table. I don't have SQL Server 2016 so I need to do this in my ASP.NET Webpages site using razor. I was able to get the file data and I used Json.Decode to make it a object I guess?? Here is how I got the JSON file data
#{
Layout = "~/_SiteLayout.cshtml";
var json =File.ReadAllText(Server.MapPath("~/App_Data/enforcement_actions.json"));
var data = Json.Decode(json);
}
I am completely lost from this point as far as now taking the 'data' variable, parse, and send to my database file. Any help would be great.
Look for Entity Framework and mdf file here: Entity Framework on .mdf file
After that, you have to look to how to map classes on Entity Framework. You see that here: How to map an Entity framework model to a table name dynamically
And finally you'll save your data on the database. You can see it here: Insert data using Entity Framework model

ASP.NET OData query returns properties with empty values for virtual properties in code first models

I'm currently building a ASP.NET OData service which uses code first with EF5. When I do the query of a entity, it is returned as JSON along with empty values for the related entities.
I want the related entities attributes not to be included in the returned JSON for the entity query unless 'Include' is explicitly mentioned.
Is there a way to achieve this?
You have to enable lazy loading by setting the related members as virtual

Calling cached data (in service layer) from an ASP.NET MVC View - what other alternative do we have?

We are adding tooltips to our ASP.NET MVC product, and we are getting the text from our database (technically, from a cached copy of the data). To do so, we created an Html Helper method:
<%=Html.Tooltip(Model.GetTooltipText(Tooltips.ClientPage.StartDateId))%>
The GetTooltipText method is in our BaseViewModel, and simply uses the passed in Id to fetch the tooltip from cache.
Would this be considered a bad design? What other alternatives would we have?
Thanks!
Its probably a better idea to grab all the Tooltip's in one hit and put them in some sort of strongly-typed collection (perhaps a Dictionary<id,string>), cache all of that in your service layer.
Then you could put this in a ViewModel and pass it through to your strongly-typed view.
In your View you could simply access that strongly-typed collected via the Model based on the unique key?
I.e.
<%: Model.Tooltips[SomeDateId] %>

ASP.NET MVC - getting started with Linq and SQL procedure

Today is my first day using ASP.NET MVC, and I'm finding it very intriguing. I only just started learning asp.net.
So basically I'm trying to call a procedure from an MSSQL database, and with it I need to send a paramater "PlaceID", which is an integer. This procedure basically just picks out a number of columns from different tables in the database. Here is the Linq to SQL code
The ultimate goal is to be able to retrieve all the information and return it as JSON with a function that will be available for a javascript.
I'm wondering what is the best way to proceed from here. I know I have to create a view, but I'm still unclear exactly on how I can call the procedure and make it store all the information. I could really use some help on this, some code examples would be excellent. I already wrote a C#.net function to convert a datatable to JSON using a stringbuilder, but I get the feeling there is a smarter way to do things.
Any help is very appreciated.
Might I suggest going through the NerdDinner.com example first. I really think that you may have started down the wrong road and it would be worth backing up and doing some review first. For one thing, you probably don't want to be mixing DataTables and LINQ, typically you'd work with strongly-typed models and have your data context/repository return IQueryable/IEnumerables of the model instead of a DataTable. For another, there is a controller method that is able to turn a model into JSON for you. Generally, you should need to write your own JSON serialization.
Declare a list and serialze it into json.
Here Lstcustomeris a genric list of class type customer
I Assign values to class varable and then insert this class in list of Lstcustomer
e.g.
Dim js As New System.Web.Script.Serialization.JavaScriptSerializer
Return js.Serialize(Lstcustomer)

How to call stored proc from ASP.Net MVC stack via the ORM & return them in json?

i'm a total newbie with asp.net mvc and here's my jam:
i have a 3 level list box which selection on box A shows options on box B and selection on box B will show the options for box C.
I'm trying to do the whole thing in asp.net MVC and what i see is that the nerd dinner tutorial uses the ORM method.
so i created a dbml to the database and drag the stored proc inside.
i create a datacontext object but i don't quite know how to connect the result from the stored proce which should be multiple rows of data and make it into a json.
so i can keep all the json data inside the html page and using jquery i could make the selection process faster.
i don't expect the data inside the three boxes to change so often thus i think this method should be quite viable.
Questions:
So how do i get the stored proc part
to return the data as json?
i've noticed some tutorial online
that the json return result part is
at the controller and not at the
model end.
Why is that?
Edit
FYI, i find what i mostly wanted to do here.
For the json part, i referenced here.
Return a JsonResult from your controller action. You may need to coerce the result from your stored procedure into a C# class serializable to Json.
Json conversion should be done in the controller because it's not really part of the domain. More a DTO in the MVVM (Model-View-ViewModel) style.

Resources