简体   繁体   English

如何为不在模型中的字段创建输入(editorfor)

[英]How to create an input (editorfor) for a field not in the model

I have the following on my EDIT view 我的编辑视图中有以下内容

@model Inspinia_MVC5.Areas.GlobalAdmin.Models.Empresa
@{
    ViewBag.Title = "Edit";
    Layout = "~/Areas/GlobalAdmin/Views/Shared/_LayoutGlobalAdmin.cshtml";
    var camposAdicionalesEmpresa = (List<Inspinia_MVC5.Areas.GlobalAdmin.Models.Propiedad>)ViewData["CamposAdicionalesEmpresa"];
    var valoresCampoAdicionalesEmpresa = (Dictionary<string, string>)ViewData["ValoresCampoAdicionalesEmpresa"];

}

and my controller is like this: 而我的控制器是这样的:

public ActionResult Edit(int? id)
{
    var listFields = from b in db.Propiedades
                     where b.Entidad.Nombre == "Empresa"
                     select b;
    ViewData["CamposAdicionalesEmpresa"] = listFields.ToList<Propiedad>();
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Empresa empresa = db.Empresas.Find(id);
    if (empresa.PropiedadesExtra != null)
    {
        XElement xmlTree = XElement.Parse(empresa.PropiedadesExtra);
        Dictionary<string, string> dict = new Dictionary<string, string>();
        foreach (var el in xmlTree.Elements())
        {
            dict.Add(el.Name.LocalName, el.Value);
        }
        ViewData["ValoresCampoAdicionalesEmpresa"] = dict;
    }
    if (empresa == null)
    {
        return HttpNotFound();
    }
    return View(empresa);
}

ViewData["ValoresCampoAdicionalesEmpresa"] is a dictionary with key values, and I need to display in an input form that value. ViewData["ValoresCampoAdicionalesEmpresa"]是具有键值的字典,我需要以输入形式显示该值。

On my razor view I have this 在我的剃刀看来,我有这个

@if (valoresCampoAdicionalesEmpresa != null)
{
    <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 camposAdicionalesEmpresa)
            {
                if (propiedad.TipoDeDatos == "Texto")
                {
                    var valor = valoresCampoAdicionalesEmpresa.Where(p => p.Key == propiedad.Nombre).First().Value;
                    <div class="form-group">
                        @Html.Label(propiedad.Nombre, new { @class = "control-label col-md-2" })
                        <div class="col-md-10">
                            @Html.Editor(valor)
                            @*@Html.ValidationMessageFor(prop => propiedad.)*@
                        </div>
                    </div>
                }
            }
        </div>
    </div>
}

If I debug, the valor variable is correctly set, however on the user interface, the textbox is empty. 如果我调试,则valor变量设置正确,但是在用户界面上,文本框为空。

How can I achieve that? 我该如何实现?

如果您没有使用编辑器的特殊原因,则将@Html.Editor(valor)替换为

@Html.TextBox("TextBoxName",valor)

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

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