0

I have a Class in my MVC app which I am calling in my contoller and getting values fill in my view

public class Mainclass
    {
    public List<main> mainset = new List<main>();
    public void Crudmain(string path) //Capital "C" => Create, "R" => Read, "U" => update
    {
        XDocument x = new XDocument(new XElement("mainset"));
        foreach (main main in mainset)
        {
            x.Root.Add(mainxml(main)); //mainxml creates xelements for main
        };
        x.Save(path + "/" + 0 + ".xml");
    }
     public class main
    {
        public personalinfo info { get; set; }
        public addressinfo currentaddr { get; set; }
        public addressinfo otheraddr { get; set; }
        public telephone currenttel { get; set; }
        public telephone othertel { get; set; }
     }

In my controller I call Crudmain() in the action.

        private main cb = new main(); 
        [HttpPost]
        public ActionResult Create(string button, main x)
        // getting path via some long code
        if (ModelState.IsValid)
        {
        cb = x;
        cb.Crudmain(path);
        return View("Read", cb);
        }
        else
        {
         return View("Create", cb);
        }

I am getting the XML file but the data is empty. Even the Path which is dependent on the variable of a object in Personalinfo is correct but the data is empty. I have a feeling that the Crudmain is initializing a new main class. I have just transitioned from VB to C# and do not understand the methods very well can someone please help me figure out how to set the Mainclass to the one passed by controller in my code.

3
  • 2
    This would be a better question if you gave a SSCCE and a description of what it should have done. Commented Jul 3, 2013 at 13:26
  • For most intents and purposes there are much better ways to create xml documents in .net. I agree with Kendall, give a description or example of the output and there should be a better way to do this. Commented Jul 3, 2013 at 13:31
  • Basically I just want a method so that the Crudmain(path) saves the XML with the datainside. Commented Jul 3, 2013 at 13:32

1 Answer 1

1
private main cb = new main(); 

public ActionResult Create(string button, main x)

cb = x;

You're overwriting cb with x. I assume x is actually blank. But it's very hard to tell, because your code is not formatted or named well. If cb is of type main, then it doesn't have a Crudmain() function - that function seems to only exist on the Mainclass class.


Another issue is that you're never putting any data into mainset in the Mainclass function. You assign it a new list, but never do anything with it. But that may just be because of the confused way your classes are formatted here. cb.Crudmain(path); still appears to be entirely invalid.

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

3 Comments

X is blank when it is passed to the view but user fills it up. I have verified that once returned by the view it is not blank anymore.
@FloodGravemind - The code's not really clear enough for us to be able to help, but I edited another suggestion into my answer.
Apparently I didnt know how get set works in C and so my mainclass only had fields and no property. Thank you very much for your help.

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.