私はすべてのレスポンスの横にあるボタンを動作させたいという私の剃刀の中に次のコードを書いてください。For Loop in RazorビューでのJavascriptボタン
クリックすると、実際の応答に応答をコピーします。
しかし、私はあなたがループのため
Microsoft JScript runtime error: Unable to get value of the property 'foo': object is null or undefined.
@for (int i = 0; i < Model.Responses.Count(); i++)
{
<div class="col-md-4">
@Html.LabelFor(x => Model.Responses[i].Response, htmlAttributes: new { @class = "control-label"})
@Html.DisplayFor(x => Model.Responses[i].Response)
</div>
<input type="button" name="set_Value" id="@i" value="Copy Response to Actual Response" class="btn btn-success" dataset-foo="@Model.Responses[i].Response" onclick="setVale(this)" />
<div class="col-md-4">
@Html.LabelFor(x => Model.Responses[i].ActualResponse, htmlAttributes: new { @class = "control-label"})
@Html.TextAreaFor(x => Model.Responses[i].ActualResponse, htmlAttributes: new { @class = "from-control", @id = "actualResponse-" + i, rows = 5, columns = 40})
</div>
<script type="text/javascript">
function setValue(btn)
{
var id = btn.id;
var value = btn.value;
var foo = btn.dataset.foo;
document.getElementById('actualResponse-' + id).value = foo
}
</script>
}
「actualResponse」というIDを持つ複数の要素があります。現在のインデックスを追加することで、それらを一意にする必要があります。 actualResponse0、actualResponse1 ... –
@RobPurcell Iveは、Iveが現在どのように動作しているかを示すために投稿を修正しましたが、ActualResposesのどれかに最後の応答しかコピーしませんでした。 –