简体   繁体   English

如何从属性而不是直接在模型中获取ASP.NET MVC中的表单值

[英]How to get form values in asp.net mvc from properties not directly in the model

I am trying to create a form which will have some standard properties based on the model, but the user will be able to extend those properties. 我正在尝试创建一个基于模型的表单,该表单将具有一些标准属性,但是用户将能够扩展这些属性。

So I have an Entity called Entity, another entity called Property, and another Entity called Company. 因此,我有一个称为Entity的实体,另一个称为Property的实体和另一个称为Company的实体。 An Entity can have a list of properties (like twitter handle, website, etc). 实体可以具有属性列表(例如Twitter句柄,网站等)。 However I want to save the values of those extended properties in an XML field on the company entity. 但是,我想将这些扩展属性的值保存在公司实体的XML字段中。

To illustrate I will paste the model here. 为了说明,我将在此处粘贴模型。

  public class Entidad
        {
            [Key]
            public int Id { get; set; }
            public string Nombre { get; set; }

            public virtual ICollection<Propiedad> Propiedades { get; set; }
        }

public class Propiedad
    {
        [Key]
        public int Id { get; set; }

        public virtual Entidad Entidad { get; set; }

        public string Codigo { get; set; }
        public string Nombre { get; set; }
        public string TipoDeDatos { get; set; }
    }

 public class Empresa 
    {
        [Key]
        public int Id { get; set; }
        public string Nombre { get; set; }
        public string NIT { get; set; }
        public string NombreRepresentanteLegal { get; set; }
        public string TelefonoRepresentanteLegal { get; set; }
        public string NombreContacto { get; set; }
        public string TelefonoContacto { get; set; }

        [Column(TypeName = "xml")]
        public string PropiedadesExtra { get; set; }

    }

On my create view I have the following: 在我的创建视图中,我具有以下内容:

@model Inspinia_MVC5.Areas.GlobalAdmin.Models.Empresa
@{
    ViewBag.Title = "Create";
    Layout = "~/Areas/GlobalAdmin/Views/Shared/_LayoutGlobalAdmin.cshtml";
    var propiedades = (List<Inspinia_MVC5.Areas.GlobalAdmin.Models.Propiedad>)ViewData["CamposAdicionales"];
}

and I render the custom attributes like this: 然后我渲染自定义属性,如下所示:

<div class="panel panel-default">
                                <div class="panel-heading">Propiedades adicionales</div>
                                <div class="panel-body">
                                    @foreach (Inspinia_MVC5.Areas.GlobalAdmin.Models.Propiedad propiedad in propiedades)
                                    {
                                        if (propiedad.TipoDeDatos == "Texto")
                                        {
                                            <div class="form-group">
                                                @Html.Label(propiedad.Nombre, new { @class = "control-label col-md-2" })
                                                @*@Html.LabelFor(prop => propiedad.Nombre, new { @class = "control-label col-md-2" })*@
                                                <div class="col-md-10">
                                                    @Html.Editor(propiedad.Nombre)
                                                    @Html.ValidationMessageFor(prop => propiedad.Nombre)
                                                </div>
                                            </div>
                                        }
                                    }

                                </div>
                            </div> 

On the empresas controller Create action I have this: 在empresas控制器上,执行以下操作:

 public ActionResult Create()
        {
            var listFields = from b in db.Propiedades
                                             where b.Entidad.Nombre == "Empresa"
                                             select b;

            ViewData["CamposAdicionales"] = listFields.ToList<Propiedad>();

            return View();
        }

And this renders a nice form like this: http://screencast.com/t/7HsI6ci9GVdo 这呈现了一个很好的形式,如下所示: http : //screencast.com/t/7HsI6ci9GVdo

However, what I dont know, is how to take those dynamic form fields and take those values to save them in the XML field from the Empresa(company) entity. 但是,我不知道的是如何采用这些动态表单字段并采用这些值以将它们保存在Empresa(company)实体的XML字段中。

呈现页面后,单击view-source,然后查看创建的输入字段的名称,它应该是每个propiedad.Nombre中的值。在服务器端,您可以通过Request["fieldname"]访问该字段的值Request["fieldname"] ,因此对于每个项目,其价值取决于

Request[propiedad.Nombre]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM