I need to create a mvc application which contain on grid view, that contains the data of the xml file, so I need to read xml file data and display that data in grid using mvc
We cannot write code for that. But I can suggest you that use XMLSerialization and map all XML data to Property Class and pass object of that property class to view as a model.
And in your view you can simply use all properties of that model.
References:
How to serialize and deserialize XML
List Model Binding in MVC
Related
I have a mvc controller which have a static page with table html tags. Now I have datas saved under sql which I wanted to replace the data.
For instance, I have a this kind of the view page Example then with the help of cs file I can change the header or footer for instance based on the data from database
How can I be able to modify the tags? As I am new for this I need some guidance regarding this?
Just a request if you think this not a related post then instead degrading it please let me know I will delete it.
Trying using ViewBag or ViewData from the controller to display values in the table, then use a conditional in C# Razor to check the data and modify your tags based on what comes from the ViewBag and ViewData.
More information here: http://weblogs.asp.net/scottgu/asp-net-mvc-framework-part-3-passing-viewdata-from-controllers-to-views
Another option is to use JavaScript to check for values from POST method.
I'm creating a ASP.NET MVC 4 web app with a database and Entity Framework 5 for a web form to maintain Document (aka Contract) entries.
Below is a sample ERD (in the form of edmx diagram with navigational properties) I have created for a DB in SQL server. For MVC web app the Model is generated from the DB with .edmx file.
Based on the Model, I have setup a form which can create and edit a Document like so:
Document Create and Edit View
This view refers to Document model class. So all the input fields using html helper (Html.TextBoxFor...,etc) are referred from Document object and its related objects (Section 1 & Section 2). The checkbox hides and shows the subform for a section with JS/jQuery. For the Edit view I use same as Create view, except it contains additional hidden ID fields to identity the records to edit from the DB.
I have added a variable length list for section item addition by following this:
http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/
In the partial view for the items dynamic list I use Section1Item and Section2Item as model reference.
Document Controller's Create method
So on Create the document object posted in Create(...) method contains all the attributes from the Document including the section's attributes.
If the checkbox value for Section 1 & Section 2 are false then I set the Document's Section 1 & Section 2 to null like so:
if (section1_chkbox == false) {
document.Section1 = null;
}
I have to add each item from the dynamic list into the document object like so:
foreach(Section1Item item in itemsList) {
document.Section1.Section1Items.Add(item);
}
So finally the Create method in the DocumentController adds the document in a db context and saves changes. The document and its related entities are successfully added to database tables.
Document Controller's Edit method
But when I'm editing the document with the Edit method in DocumentController there are some inconsistencies with some section1 object's ID being null for some related objects in Section1Items. For example when Section1Items are present the posted Document object (for edit) does not contain reference to Section 1 object's ID.
So it is fiddly and messy to update an existing document record as I'm having to check the ID attributes of a object. There are always errors like inconsistent principal and dependent objects in the relationship when saving or setting the object's entry state to modified.
My Question
How can I setup the View and Controller to achieve a functioning Create and Edit of a document along with its related entities as in the form's interface able? I need a efficient and correct way to use Entity Framework for this. How can I use view models for this if that would make this easier?
From what you described here I understand that there is a problem with virtual reference to Section1 from Section1Items. So in other words the problem is that entity framework model doesn't suit you display needs. You would need to render duplicated entities to map entier Document in your view. Am I right?
If that's the case I see two solutions:
like you suggested use view models to solve the problem. If you confirm the issue I can try to write example view model.
serialzie/encrypt entier Document in your view and next merge edited result with encrypted Document. You can achive that by using Mvc3Futures feature.
Serialize and encrypt your document in view:
#Html.Serialize("Document", Model, SerializationMode.EncryptedAndSigned)
And to deserialzie and decrypt you can use:
public ActionResult Edit(string id,[Deserialize(SerializationMode.EncryptedAndSigned)]Document document)
I am working with knockout and mvc. I make a call to an mvc controller to retrieve json that I create a model object from and apply it to a div:
myModel = ko.mapping.fromJS(items);
ko.applyBindings(myModel , document.getElementById("my-container"));
I am wondering how can I extend my myModel object (which is populate from json from the controller) to have custom properties based upon values on the object returned from my controller?
Generally you would want to use the create callback as described in this section of the documentation: http://knockoutjs.com/documentation/plugins-mapping.html#customizing_object_construction_using_create
It is also possible to simply add properties to myModel after creating it via the mapping plugin in your example.
I am looking for solution to inject data annotation attributes to ViewModels in MVC. The idea is decorate the ViewModel class with Related Entity and the edmx file path and then read the Edmx file and the entity metat data information. Once entity meta data identified iterate through the properties of the view model, locate the matching entity attribute then decorate the view model with RequiredAttribute, StringLengthAttribute etc based on entity information.
Is it possible to use the t4 templates, since I have to read teh existing .cs file created by the user, modify and Save it.
How can i achieve this? Pls advice.
I was wondering if there is any way to save form values in an xml format and reload it later.
I want to pass in my model (containing all form values) and save the form content in an xml file.
Many thanks
Sandra
Found a way to do this by serializing my model to xml by using the DataContractSerializer and them save the MemoryStream