0

I am working on a project where I have to add data to database using XML parsing. Data is coming from View to Controller in Details. How can I save it?

I have no idea how to do that. This is how I tried to achieve it:

Controller

[HttpPost]
[ValidateInput(false)]
public ActionResult Add(SaleInvoice obj, string details)
{
    var objsi = new SaleInvoice().Add(obj);

    return View();
}

View Jquery Function

$("#btnsave").click(function () {
        var xmlstring = "<?xml version=\"1.0\"?>";
        xmlstring += "<Sale_Invoice>"
        var index = 0;
        $("#tbldetail tbody").find("tr").each(function (i, ef) {

            xmlstring += "<Detail>";
            xmlstring += "<Product>" + $(ef).children().get(0).innerText + "</Product>"
            xmlstring += "<Quantity>" + $(ef).children().get(3).innerText + "</Quantity>"



            index = index + 1;


        });

        $('#frmAdd').prepend("<input type='hidden' name='details' value='" + xmlstring +"' />");

    $('#frmAdd').submit();
});

});

1 Answer 1

1

You need to post your XML string to a controller action to allow you to hook into your database service so you can save it. I'd use an AJAX post like shown in the example below:

          $.ajax({
                type: "POST",
                url: "@Url.Action("YourSaveAction", "YourController")",
                data: { xmlString: myXMLVar}
            });

Looking at what you are attempting, I'd much sooner handle the XML manipulation outside of JQuery. I'd look to use some form of XML serialization inside your application in order to represent the model of your sales and invoices. You can then manipulate strongly typed objects rather than extracting properties via JQuery.

Below is an example of how you could do this.

First create a class marked as serializable to store your order details.

    [XmlRoot]
    [Serializable]
    public class SalesInvoice
    {
        [XmlElement]
        public List<Detail> Details { get; set; }

        public SalesInvoice()
        {
            Details = new List<Detail>();
        }
    }

    [Serializable]
    public class Detail
    {
        [XmlElement]
        public string Product { get; set; }

        [XmlElement]
        public string Quantity { get; set; }
    }

Assuming you have raw XML somewhere, you can then serialize your XML into a Sales class. For this example I'm reading the XML locally from a file, but you can plug in your source.

            var configStream = File.OpenRead(@"C:\invoice.xml");
            var reader = new StreamReader(configStream);
            var xmlString = reader.ReadToEnd();
            var stringReader = new StringReader(xmlString);
            var newInvoice = (SalesInvoice)serializer.Deserialize(stringReader);

This will then give you a strongly typed object which you can now use to write the data out to the database.

For your MVC application, I would have a ViewModel something like the example below:

 public class SalesModel
 {
     public SalesInvoice Invoice { get; set; }
 }

This is what could be manipulated in your View and finally posted to a controller to save the data to the database.

Your controller might look something like this:

        [HttpPost]
        public void SaveSalesInvoice(SalesModel model)
        {
            var myDataService = new DataService();
            myDataService.SaveSalesInvoice(model.Invoice);

        }

I presume you would have some kind of service in the example above that takes the SalesInvoice object as a parameter allowing you to save its data to the database.

Having re-read again, it looks like you may want to save the whole XML into the database. If this is the case, then you can de-serialize the object created to get the XML string and save that.

var serializer = new XmlSerializer(typeof(SalesInvoice));
var writer = new StringWriter();
serializer.Serialize(writer, invoice);
var xml = writer.ToString();

Well I hope this makes sense and helps....and that I've understood your need :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.