简体   繁体   中英

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. An Entity can have a list of properties (like twitter handle, website, etc). However I want to save the values of those extended properties in an XML field on the company entity.

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:

 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

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.

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

Request[propiedad.Nombre]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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