简体   繁体   English

使用JavaScript创建Razor对象

[英]Create a Razor Object with JavaScript

To Long to Read (tl;dr): 要阅读(tl; dr):

Make this add a new cell to a table that shows a razor/mvc dropdown: 将此单元格添加到显示剃刀/ mvc下拉列表的表格中:

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

Long but more detailed question: 长但更详细的问题:

Pretty simple answer, I think, but I'm just too new to Razor to know. 我认为这是一个非常简单的答案,但我对Razor来说太新了。

Currently, I have a table that has rows added using JavaScript by means of a 'add row' button. 目前,我有一个表,通过“添加行”按钮使用JavaScript添加行。

I actually asked the question before, and found a solution that worked perfectly. 我之前实际上问了这个问题,并找到了一个完美的解决方案。 However, now I need it in Razor so my previous question can't work. 但是,现在我需要它在Razor中,所以我之前的问题无法奏效。

Here's the link with pictures to my other post: 这是与我的其他帖子的图片链接:

Create Dynamic Elements that are MVC Format 创建MVC格式的动态元素

Ok so new issue. 好的,这么新的问题。 I have a razor element that needs to be duplicated with JavaScript. 我有一个需要与JavaScript重复的剃刀元素。 I've changed the names of the classes and properties. 我已经更改了类和属性的名称。

So in the table, I have: 所以在表中,我有:

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

Which works great. 哪个效果很好。 The table populates with this in it just fine. 表中填充了这个就好了。 However, like in my other post, I need to replicate the entire line with JavaScript (or whatever other solution there is). 但是,就像在我的其他帖子中一样,我需要使用JavaScript(或其他任何解决方案)复制整行。

Currently, I have this style of insert: 目前,我有这种插入风格:

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

The type changes from text to email to tel. 类型从文本更改为电子邮件到tel。 The '#' in name = "[#].chkbox" actually is a variable that is passed through, so each of the # are different. name = "[#].chkbox"的'#'实际上是一个传递的变量,因此每个#都是不同的。 Somehow, someway, I want to do: 不知何故,我想做:

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

Note: the rendered version keeps turning out like this 注意:渲染版本不断变形

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

So I was wrong. 所以我错了。 MVC will read select boxes that aren't in razor. MVC将读取不在剃刀中的选择框。 So that's good. 这很好。

It was a naming issue. 这是一个命名问题。 I didn't figure it out, but a co-worker with WAY more experience than me was nice enough to help. 我没弄明白,但是与我相比有更多经验的同事很乐意提供帮助。

Basically, we made one drop down based on the model using Razor, and then stole the options of that drop down to make the other drop downs. 基本上,我们使用Razor基于模型进行了一次下拉,然后偷走了下拉的选项以使其他下降。

We made a list of contacts in the big class, and named all our elements in this style: 我们在大班中列出了联系人列表,并以此样式命名了所有元素:

name = "NameOfListElement[listNumber].ClassProperty"

name = "Contacts[0].FullName"

Class 1: 第1类:

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

Class 2: 第2类:

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

HTML super abridged. HTML超级删节。 Putting in the name for the Razor syntax is important: 输入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 will create this, which is important for the ID: 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: 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: 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);
}

For more info on the Controller and stuffwhich just populates the DropDown right now and does nothing else, see my other question: Create Dynamic Elements that are MVC Format 有关控制器的更多信息以及现在只填充DropDown并且不执行任何操作的内容,请参阅我的另一个问题: 创建MVC格式的动态元素

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

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