Я пытаюсь передать несколько ссылок с их собственными параметрами контроллеру из View Но я получаю нулевые значения, проанализированные контроллером. Ниже приведены основные фрагменты кода. Любая помощь будет высоко оценена.
@foreach (var item in Model) {
<a href="#[email protected]&[email protected]" class="list-group-item active myajaxlink" >
<span class="label label-inverse pull-right">@item.InOn</span>
<h4 class="list-group-item-heading">@item.SenderUn @item.Subject<i class="fa fa-flag text-primary"></i></h4>
<p class="margin-none">@Html.DisplayFor(modelItem => item.Body)</p>
</a>}
Ниже приведен Javascript с Ajax
<script type="text/javascript">
$(document).ready(function(){
$("#animatedgif").hide();
$(".myajaxlink").click(function () {
$.ajax({
url: "/InMail/check",
type: "POST",
data: { id: $('#id').val(), un: $('#un').val() }
}).done(function () {
}).complete(function (){
$("#animatedgif").hide();
}).success(function (dv) {
$(".email-content").replaceWith(dv);
});
});
});
$(document).ajaxStart(function () {
$("#animatedgif").show();
}).ajaxSuccess(function () {
$("#animatedgif").hide();
});
function myonbegin() {
//show animated gif
//alert("helloworld");
$("#animatedgif").show();
}
function myonsuccess() {
//disable animated gif
$("#animatedgif").hide();
}
function myonfailure() {
//disbale animated gif
$("#animatedgif").hide();
}
function myoncomplete() {
//disable animated gif
$("#animatedgif").hide();
}
</script>
Вы получаете null, потому что $('#id').val()
и $('#un').val()
возвращает null. Это происходит потому, что эта конструкция $('#id').val()
ищет элемент на странице с атрибутом id = "id" и для #un - id = "un".
Чтобы получить то, что вам нужно, вы можете просто добавить следующие изменения:
Razor:
@foreach (var item in Model) {
<a href="#" data-id="@item.Id" data-un="@item.ReceiverUn" class="list-group-item active myajaxlink" >
<span class="label label-inverse pull-right">@item.InOn</span>
<h4 class="list-group-item-heading">@item.SenderUn @item.Subject<i class="fa fa-flag text-primary"></i></h4>
<p class="margin-none">@Html.DisplayFor(modelItem => item.Body)</p>
</a>
}
Javascript:
$(".myajaxlink").click(function (e) {
e.preventDefault();
var link=$(this);
$.ajax({
url: "/InMail/check",
type: "POST",
data: { id: link.attr("data-id"), un: link.attr("data-un") }
})
//handle .done, .fail and etc.
});