Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将ID从Foreach传递到Html.BeginForm_Javascript_C#_Html_Model View Controller - Fatal编程技术网

Javascript 将ID从Foreach传递到Html.BeginForm

Javascript 将ID从Foreach传递到Html.BeginForm,javascript,c#,html,model-view-controller,Javascript,C#,Html,Model View Controller,在提交表单时,我需要从我的模型中循环foreach以传递和Id@item.errorId,因为提交的表单必须追溯到确切的行 我尝试创建一个单页应用程序 这是我创建表的CSHTML <tbody> @foreach (var item in Model.LogItems) { <tr href="#/" onclick="createTicket('@HttpUtility.JavaScript

在提交表单时,我需要从我的模型中循环
foreach
以传递和Id
@item.errorId
,因为提交的表单必须追溯到确切的行

我尝试创建一个单页应用程序

这是我创建表的CSHTML

   <tbody>
            @foreach (var item in Model.LogItems)
            {
                <tr href="#/" onclick="createTicket('@HttpUtility.JavaScriptStringEncode(item.Type)', '@HttpUtility.JavaScriptStringEncode(item.TimeUtc.ToString())', '@HttpUtility.JavaScriptStringEncode(item.Host)', '@HttpUtility.JavaScriptStringEncode(item.Message)', '@HttpUtility.JavaScriptStringEncode(item.Source)', '@HttpUtility.JavaScriptStringEncode(item.User)', '@HttpUtility.JavaScriptStringEncode(item.AllXml)', '@HttpUtility.JavaScriptStringEncode(item.ErrorId.ToString())')" style="cursor: pointer">
                    <td scope="row"> @(rowNo += 1) </td>
                    <td style="white-space: nowrap;">
                        @item.TimeUtc
                    </td>
                    <td style="word-wrap:break-word; max-width: 250px;">
                        @item.Type
                    </td>
                    <td>@item.Host</td>
                    <td style="max-width: 200px; word-wrap:break-word;"> @item.Message</td>
                    <td>
                        @item.Source
                    </td>
                    @if (item.User != "")
                    {
                        <td>@item.User</td>
                    }
                    else
                    {
                        <td><i>null</i></td>
                    }
                    <td>@item.AllXml.Split('"')[1].Split('"')[0]...</td>
                    <td style="white-space: nowrap;"> <button class="btn btn-link btn-xs">Dismiss</button></td>
                </tr>
            }
        </tbody>
单击该行后,我们得到以下结果

我们可以点击票证标签

这是CSHTML

 @using (Html.BeginForm("Index", "Ticket", FormMethod.Post))
            {
                <div>
                    <br />
                    <div style="background-color:#2C3E5070; color: white; padding: 3px; border-radius:3px; font-weight: 300;">
                        <a style="font-size: 0.9em">Create Issue</a>
                    </div>
                    <br />
                    <form>
                        <span style="font-size: 0.8em">Project<sup class="star">*</sup></span> @Html.DropDownListFor(m => model.fields.project.key, (IEnumerable<SelectListItem>)ViewBag.Projects, "<select>", new { @class = "form-control input-sm my-size-text-area input-background" })
                        <br />
                        <span style="font-size: 0.8em">Issue type</span> @Html.DropDownListFor(m => model.fields.issuetype.name, new List<SelectListItem> { new SelectListItem { Text = "Bug", Value = "Bug" }, new SelectListItem { Text = "Feature", Value = "Feature" }, new SelectListItem { Text = "Task", Value = "Task" }, }, new { @class = "form-control input-sm my-size-text-area input-background" })
                        <br />
                        <span style="font-size: 0.8em">Assignee<sup class="star">*</sup></span> @Html.DropDownListFor(m => model.fields.assignee.name, (IEnumerable<SelectListItem>)ViewBag.Users, "<select>", new { @class = "form-control input-sm my-size-text-area input-background" })
                        <br />
                        <span style="font-size: 0.8em">Summary<sup class="star">*</sup></span>@Html.TextBoxFor(m => model.fields.summary, new { @class = "form-control input-sm my-size-text-area input-background" })
                        <br />
                        <div class="form-group">
                            <span style="font-size: 0.8em">Description<sup class="star">*</sup></span> @Html.TextAreaFor(m => model.fields.description, 5, 60, new { @class = "form-control my-size-text-area input-background" })
                        </div>
                        <br />
                        <input style="font-size: 0.8em !important;" onclick="loadingOverlay()" id="Submit" class="btn btn-primary float-right" type="submit" value="Create" />
                    </form>
                </div>
            }
但这将始终传递来自最后一个
foreach
项的id

我感谢所有的帮助

试试这个

  • cshtml
  • 访问控制器中的隐藏字段值

  • 请澄清你到底在问什么。我已经编辑了这篇文章并尽可能地澄清了。thank youTry click事件,在类名为的行上声明您自己的属性,并使用“this”关键字在javascript中访问所有属性。这可能可以解决您的问题。这将返回一个空日志`jQuery(document).ready(函数($){$(“.rowId”)。单击(函数(){var id=$(This.val();控制台.log(id)});`但是我可以只记录这个,它会返回一个完整的元素,在里面我们得到value=“”,但我不知道如何访问它,你不会像这样。在custome\attribute中传递errorid,然后像
    一样访问它。我已经更正了我的帖子,澄清ID在这种情况下不是行号,而是
    @item.errorid
    ,如果我尝试像这样传递它
    onclick=“createTicket('@HttpUtility.JavaScriptStringEncode(item.errorid)…”
    它将始终是表格的最后一个id确定…然后您可以尝试在
    cshtml
    中添加隐藏字段…隐藏字段将作为表单的一部分发送给控制器
     @using (Html.BeginForm("Index", "Ticket", FormMethod.Post))
                {
                    <div>
                        <br />
                        <div style="background-color:#2C3E5070; color: white; padding: 3px; border-radius:3px; font-weight: 300;">
                            <a style="font-size: 0.9em">Create Issue</a>
                        </div>
                        <br />
                        <form>
                            <span style="font-size: 0.8em">Project<sup class="star">*</sup></span> @Html.DropDownListFor(m => model.fields.project.key, (IEnumerable<SelectListItem>)ViewBag.Projects, "<select>", new { @class = "form-control input-sm my-size-text-area input-background" })
                            <br />
                            <span style="font-size: 0.8em">Issue type</span> @Html.DropDownListFor(m => model.fields.issuetype.name, new List<SelectListItem> { new SelectListItem { Text = "Bug", Value = "Bug" }, new SelectListItem { Text = "Feature", Value = "Feature" }, new SelectListItem { Text = "Task", Value = "Task" }, }, new { @class = "form-control input-sm my-size-text-area input-background" })
                            <br />
                            <span style="font-size: 0.8em">Assignee<sup class="star">*</sup></span> @Html.DropDownListFor(m => model.fields.assignee.name, (IEnumerable<SelectListItem>)ViewBag.Users, "<select>", new { @class = "form-control input-sm my-size-text-area input-background" })
                            <br />
                            <span style="font-size: 0.8em">Summary<sup class="star">*</sup></span>@Html.TextBoxFor(m => model.fields.summary, new { @class = "form-control input-sm my-size-text-area input-background" })
                            <br />
                            <div class="form-group">
                                <span style="font-size: 0.8em">Description<sup class="star">*</sup></span> @Html.TextAreaFor(m => model.fields.description, 5, 60, new { @class = "form-control my-size-text-area input-background" })
                            </div>
                            <br />
                            <input style="font-size: 0.8em !important;" onclick="loadingOverlay()" id="Submit" class="btn btn-primary float-right" type="submit" value="Create" />
                        </form>
                    </div>
                }
    
    @using (Html.BeginForm("Index", "Ticket", FormMethod.Post))
    
    @Html.HiddenFor(model => m.errorId)