【问题标题】:Is it possible to use HTML Helpers in Javascript?是否可以在 Javascript 中使用 HTML Helpers?
【发布时间】:2019-01-10 23:04:06
【问题描述】:

我正在为我工​​作的公司开发一个 Web 应用程序。用户可以通过设置他们想要使用的小时数来动态地在 HTML 表中创建行。这是通过 Javascript 实现的。

例如,如果用户输入数字 5,表格中将显示 5 行。

如何将我的模型与动态组件一起使用?

我已经尝试在脚本中添加这个:

@Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control", id = "priceInput", type = "number" } })

代替:

const priceInput = document.createElement('priceInput');

查看下面的完整脚本。

<script type="text/javascript">
        function changeRow() {
            const aTable = document.getElementById('hourPriceTable');
            const hourTextBox = document.getElementById('hourInput');
            const rowNumber = parseInt(hourTextBox.value);
            if (rowNumber) {
                const rows = [];

                for (let i = 1; i <= rowNumber; i++) {
                    if (rowNumber < 1 || rowNumber > 24) {
                        document.getElementById('error').innerText = ('O número de horas precisa de ser entre 1 e 24.');
                        document.getElementById('error').style.color = 'red';
                        return false;
                    } else {
                        document.getElementById('error').innerText = '';
                    }
                    const row = document.createElement('tr');
                    const th = document.createElement('th');
                    const td2 = document.createElement('td');
                    th.setAttribute("class", "text-center");
                    td2.setAttribute("class", "text-center");
                    const priceInput = document.createElement('priceInput');
                    priceInput.type = 'number';
                    priceInput.setAttribute("class", "text-center");
                    th.append(i);
                    td2.append(input);
                    row.append(th);
                    row.append(td2);
                    rows.push(row);
                }
                aTable.innerHTML = "";
                rows.forEach(row => aTable.append(row));
            }
        }
    </script>

我想要我用来处理插入到这些动态行中的数据(存储到数据库)的模型,因为它们里面有文本框。我一直在努力添加此功能,因为我无法使用 Html.EditorFor 在 JavaScript 代码中创建文本框。

感谢任何帮助,如果这令人困惑,我很抱歉。

【问题讨论】:

  • 服务器页面运行(执行 C# 方法,如 Html.EditorFor)并生成 HTML。客户端浏览器呈现 HTML 并执行 JavaScript。请注意,有一个执行上下文的严格分离,并且以“通过某种方法”结束在 HTML(或 JavaScript 资源,例如)上的所有东西(并且只有东西)都可以在客户端上使用.
  • 检查This answer
  • 我想你是在问如何在 post 上绑定动态模型。对吗?
  • 我很欣赏到目前为止的回复。我找到了@NaDeRStar 指出的答案,但我不理解 MyCustomDataAttribute 位。我一般是编程新手,对我使用的技术并不完全熟悉。有可能是Khyron,我去看看!谢谢。
  • @SimãoAmaral 我们需要查看更多您的代码。你能发布模型、视图和控制器(获取和发布)。动态表显示正常还是有问题?

标签: javascript c# asp.net-mvc


【解决方案1】:

如果您的 javascript 在视图中,那么您可以将变量设置为 html 字符串:-

<script>
    var input = '@Html.EditorFor(model => model.CertificateCode, new { htmlAttributes = new { @class = "form-control", id = "priceInput", type = "number" } })';
</script>

input 现在将是输入元素的 html 字符串。

然后您可以通过将input 字符串传递给this 来创建一个js 元素:

function createElementFromHTML(htmlString) {
  var div = document.createElement('div');
  div.innerHTML = htmlString.trim();

  // Change this to div.childNodes to support multiple top-level nodes
  return div.firstChild; 
}

或者如果你有 jQuery,你可以简单地使用:

<script>
  var input = '@Html.EditorFor(model => model.CertificateCode, new { htmlAttributes = new { @class = "form-control", id = "priceInput", type = "number" } })';
  var element = $(input);
</script>

【讨论】:

  • 我尝试了 jQuery 位,但我得到的只是 HTML 文本,而不是实际的组件。我做错了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-23
  • 1970-01-01
  • 2012-08-26
  • 1970-01-01
  • 1970-01-01
  • 2017-06-07
相关资源
最近更新 更多