【问题标题】:Create a Razor Object with JavaScript使用 JavaScript 创建 Razor 对象
【发布时间】:2019-04-20 00:31:00
【问题描述】:

要读很久(tl;dr):

使这个添加一个新单元格到一个显示剃刀/mvc 下拉列表的表格中:

var element1 = document.createElement(@Html.DropDownListFor(m => m.Contact.Attribute, Model.Contact.Attributes, new { @class = "form-control" }));

冗长但更详细的问题:

很简单的答案,我想,但我对 Razor 太陌生了,不知道。

目前,我有一个表,其中包含使用 JavaScript 通过“添加行”按钮添加的行。

我实际上之前问过这个问题,并找到了一个完美的解决方案。但是,现在我在 Razor 中需要它,所以我之前的问题无法解决。

这是我另一篇文章的图片链接:

Create Dynamic Elements that are MVC Format

好的,新问题。我有一个需要用 JavaScript 复制的 razor 元素。我已经更改了类和属性的名称。

所以在表格中,我有:

<td>@Html.DropDownListFor(m => m.Contact.Attribute, Model.Contact.Attributes, new { @class = "form-control" })</td>

效果很好。表格中填充了它就好了。但是,就像在我的另一篇文章中一样,我需要使用 JavaScript(或任何其他解决方案)复制整行。

目前,我有这种插入方式:

var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
element1.type = "checkbox";
element1.name = "[#].chkbox";
element1.className = "form-control";
cell1.appendChild(element1);

类型从文本变为电子邮件再到电话。 name = "[#].chkbox"中的'#'其实是一个被传递的变量,所以每个#都是不同的。不知何故,不知何故,我想做:

var element1 = document.createElement(@Html.DropDownListFor(m => m.Contact.Attribute, Model.Contact.Attributes, new { @class = "form-control" }));

注意:渲染的版本总是这样

var element2 = document.createElement(<select class="form-control" id="InjuredPerson_SuspectedInjury" name="InjuredPerson.SuspectedInjury"><option value="A.G.E.">A.G.E.</option>

【问题讨论】:

    标签: javascript asp.net-mvc razor


    【解决方案1】:

    所以我错了。 MVC 将读取不在 razor 中的选择框。所以这很好。

    这是一个命名问题。我没弄明白,但是一个比我有 WAY 经验的同事很乐意提供帮助。

    基本上,我们使用 Razor 根据模型制作了一个下拉菜单,然后窃取该下拉菜单的选项来制作其他下拉菜单。

    我们在大类中制作了一个联系人列表,并以这种风格命名了我们所有的元素:

    name = "NameOfListElement[listNumber].ClassProperty"
    
    name = "Contacts[0].FullName"
    

    第 1 类:

    public class BigClass {
    public List<Contact> contacts {gs}
    public Contact contact {gs}
    public List<SelectedListItem> DropDownItems {gs}
     }
    

    第 2 类:

    public class Contact {
    public string FullName {gs}
    public string DropDownItem {gs}
    }
    

    HTML 超级删节。输入 Razor 语法的名称很重要:

     @model nameOfProject.Models.BigClass
    
    
        @using (Html.BeginForm("ViewName", "ViewFolder", FormMethod.Post)) {
    
        //all other form elements. make a standard table. Then add this:
    
        <tr>
        <td><input type="text" name="Contacts[0].FullName" class="form-control" /></td>
        <td>@Html.DropDownListFor(m => m.Contact.DropDownItem, Model.DropDownItems, new { 
        @class = "form-control", Name = "Contacts[0].DropDownItem" })</td>
        </tr>
         }
    

    Razor 会创建这个,这对 ID 很重要:

    <select name="Contacts[0].DropDownItem" class="form-control" 
    id="Contact_DropDownItem">
    <option value="Apple">Apple</option>
    <option value="Banana">Banana</option>
    <option value="Orange">Orange</option>
    </select>
    

    JavaScript:

    function addRow(tableID, idNumber) {
    //A lot more code up here but this is what you'd need for the dropDown:
    var cell6 = row.insertCell(5);
    var element6 = document.createElement("select");
    element6.id = "Contacts[" + idNumber + "].DropDownItem";
    element6.name = "Contacts[" + idNumber + "].DropDownItem";
    element6.className = "form-control";
    cell6.appendChild(element6);
    
    addOptions(element6);
    }
    

    JavaScript:

    function addOptions(element) {
    var x = document.getElementById("Contact_DropDownItem");
    for (var i = 0; i < x.length; i++) {
        var singleOption = x.options[i].value;
    
        var option = document.createElement("option");
        option.value = i.toString();
        option.text = singleOption;
    
        element.add(option, null);
    }
    

    有关控制器的更多信息以及现在仅填充 DropDown 并且不执行任何其他操作的内容,请参阅我的另一个问题:Create Dynamic Elements that are MVC Format

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      • 2011-11-01
      • 1970-01-01
      • 2014-05-06
      • 2015-12-06
      • 1970-01-01
      相关资源
      最近更新 更多