0

我在我的 MVC 应用程序中有一个类,我在我的控制器中调用它并获取值填充我的视图

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; }
     }

在我的控制器中,我在操作中调用 Crudmain()。

        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);
        }

我正在获取 XML 文件,但数据为空。即使是依赖于 Personalinfo 中对象变量的 Path 也是正确的,但数据是空的。我感觉 Crudmain 正在初始化一个新的主类。我刚刚从 VB 过渡到 C#,不太了解这些方法,有人可以帮我弄清楚如何在我的代码中将 Mainclass 设置为控制器传递的那个。

4

1 回答 1

1
private main cb = new main(); 

public ActionResult Create(string button, main x)

cb = x;

您正在cb覆盖x. 我认为x实际上是空白的。但这很难说,因为您的代码没有正确格式化或命名。如果cb是 type main,那么它没有Crudmain()函数 - 该函数似乎只存在于Mainclass类中。


另一个问题是您永远不会将任何数据放入mainset函数中Mainclass。您为其分配了一个新列表,但从不对其进行任何操作。但这可能只是因为您的类在此处格式化的方式令人困惑。 cb.Crudmain(path);似乎仍然完全无效。

于 2013-07-03T13:30:59.750 回答