我在我的 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 设置为控制器传递的那个。