TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Marcelo Souza
NA
25
6.3k
ASP.NET AJAX MVC - Calling Controller Method from View
Dec 16 2017 9:36 AM
Hi
I need to call a controller method (this method will return TRUE or FALSE) from my view in order to show a "hands-up or hands-down" image. Bellow you will find my code. Am I in the right path? I don't have experience with ajax so every help is appreciated.
The results below are return only one image (the first line in the table), despite of the table on my Index view returning many lines. What am I doing wrong?
What should I do to my boolean method return true or false, depending on a parameter (like IdClient)? Note that in this example, I'm always returning false (just to test it...).
Controller Method:
public bool GetTrueOrFalse()
{
return false;
}
My View:
@model IEnumerable<AutoCompleteDropdownlistTeste.Models.Cliente>
@{
ViewBag.Title =
"Index"
;
}
<h2>Index</h2>
<script src=
"~/Scripts/jquery-1.10.2.js"
></script>
<script src=
"~/Scripts/jquery.unobtrusive-ajax.js"
></script>
<style>
.wrapper {
float
: left;
clear: left;
display: table;
table-layout: fixed;
}
img.img-responsive {
display: table-cell;
max-width: 20%;
}
.icon-size {
font-size: 20px;
}
</style>
<script type=
"text/jscript"
>
$.ajax({
url:
'@Url.Action("GetTrueOrFalse", "Clientes" )'
,
success:
function
(result) {
var
str = result.toLowerCase();
if
(JSON.parse(str)) {
$(
"#ddlVerFalse"
).append(
"<a class='glyphicon glyphicon-thumbs-up icon-size text-success' href='/membroes/Edit/1' title='Editar'> </a>"
)
}
else
{
$(
"#ddlVerFalse"
).append(
"<a class='glyphicon glyphicon-thumbs-down icon-size text-danger' href='/membroes/Edit/1' title='Editar'> </a>"
)
}
}
});
</script>
<p>
@Html.ActionLink(
"Create New"
,
"Create"
)
</p>
<table
class
=
"table"
>
<tr>
<th>
Status
</th>
<th>
@Html.DisplayNameFor(model => model.NmCliente)
</th>
<th>
@Html.DisplayNameFor(model => model.Estado.NmEstado)
</th>
<th></th>
</tr>
@foreach (
var
item
in
Model)
{
<tr>
<td>
<p id=
"ddlVerFalse"
></p>
</td>
<td>
@Html.DisplayFor(modelItem => item.NmCliente)
</td>
<td>
@Html.DisplayFor(modelItem => item.Estado.NmEstado)
</td>
<td>
@Html.ActionLink(
"Edit"
,
"Edit"
,
new
{ id=item.IdCliente }) |
@Html.ActionLink(
"Details"
,
"Details"
,
new
{ id=item.IdCliente }) |
@Html.ActionLink(
"Delete"
,
"Delete"
,
new
{ id=item.IdCliente })
</td>
</tr>
}
</table>
Thanks.
Reply
Answers (
4
)
Advice on Partial view
Can't get value for DropDownList