如何在 Asp.Net MVC 2 或 3 中编辑/查看动态对象?

发布于 2024-12-21 21:58:26 字数 560 浏览 0 评论 0原文

我想使用完全由 xml 组成的对象,并且我想创建一个视图框架,允许我在 asp.net mvc 视图中编辑/查看此对象。

您知道我该如何实现这一目标吗?

任何想法都是好的。

谢谢

编辑1:xml的示例,但这是基本的,我想在这个xml中表示任何类型的数据,包括base64图片

<?xml version="1.0" encoding="UTF-8"?>
<product xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
     <name xsi:nil="true"></name>
     <description>bqddd</description>
</product>

编辑2:我想存储为对象的xml属性,每个对象具有不同的属性。当我编辑一个对象时,我希望能够为我拥有的每种类型的对象动态地显示不同类型的界面(作为 xml)。

编辑3:如果可能的话,我还希望能够动态更改视图,而无需重新编译。

I want to used objects that are entirely made of xml, and i want to make a view framework the would allow me to edit / view this object in a asp.net mvc view.

Do you have an idea on how could I accomplish this?

Any idea is good.

Thank you

Edit 1: Example of xml, but this is basic, i want to represent any kind of data in this xml, including base64 pictures

<?xml version="1.0" encoding="UTF-8"?>
<product xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
     <name xsi:nil="true"></name>
     <description>bqddd</description>
</product>

Edit 2: I want to store as xml properties for objects, each object with different properties. And when I edit an object i want to be able to show a different type of interface dinamically for each type of object i have as xml.

Edit 3: I want also to be able to change the view on the fly without the need to recompile, if posible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纵性 2024-12-28 21:58:26

所以我只是写了一些代码,不确定你想要什么。

根据您的 xml,您可以添加一个 ViewModel,例如:

class Product
{
 public string Name..
 public string Description..
}

但是您说了一些关于动态的内容,您可以使用 ExpandoObject 类。

检查此代码:

void Main()
{
    XmlDynamicModel x = new XmlDynamicModel(@"path/myobject.xml");
    //you're element should be <description>value</description>
    //I would rather capitalize the first letter **Description
    Console.WriteLine(x.TheObject.description);
    Console.WriteLine(x.TheObject.name);
}
public class XmlDynamicModel
{
    public XmlDynamicModel(string xmlfile)
    {
      this.TheObject = new ExpandoObject();
      var t = this.TheObject as IDictionary<String, object>;
      XDocument xmlDoc = XDocument.Load(xmlfile);
      //get all objects UNDER product
      foreach(var elem in xmlDoc.Descendants().Descendants())
      {
        t[elem.Name.ToString()] = elem.Value.ToString();
      }
    }
    public dynamic TheObject {get;set;}
}

您可以通过添加对象名称(在本例中为产品)作为属性并查找不同类型并设置空值等来使其更精美。

希望它有帮助

So off the bat I just wrote some code unsure of what you want.

Based on your xml you could just add a ViewModel like:

class Product
{
 public string Name..
 public string Description..
}

But then you said something about being dynamic and there is something interesting you can do with the ExpandoObject class.

Check this code:

void Main()
{
    XmlDynamicModel x = new XmlDynamicModel(@"path/myobject.xml");
    //you're element should be <description>value</description>
    //I would rather capitalize the first letter **Description
    Console.WriteLine(x.TheObject.description);
    Console.WriteLine(x.TheObject.name);
}
public class XmlDynamicModel
{
    public XmlDynamicModel(string xmlfile)
    {
      this.TheObject = new ExpandoObject();
      var t = this.TheObject as IDictionary<String, object>;
      XDocument xmlDoc = XDocument.Load(xmlfile);
      //get all objects UNDER product
      foreach(var elem in xmlDoc.Descendants().Descendants())
      {
        t[elem.Name.ToString()] = elem.Value.ToString();
      }
    }
    public dynamic TheObject {get;set;}
}

You could make it fancier by adding the object name (in this case product) as a property and looking out for different types and setting null values etc.

Hope it helps

弥枳 2024-12-28 21:58:26

由于您的产品可能有多个不同类型的内部标记,因此您应该有一个具有元数据属性的 ViewModel...,然后是一个内部属性的集合(与 XML 的内部标记相匹配)。

这样,您可以循环遍历标签集合来显示或创建要编辑的表单。

Since your product could have several inner tags of different types, you should have a ViewModel with properties of metadata... and then a collection of inner properties (that match the inner tags of your XML).

That way, you can loop through the tag collection to display or create a form to edition.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文