سلام..
لطفاً کدهای زیر رو مطالعه بفرمایید و راهنماییم کنید که اشتباهم کجاست که AJax کار نمیکنه و به ویوی GetMessage ریدایرکت میشه....
کد های کنترلر :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVCSample.Controllers
{
public class myController : Controller
{
public ActionResult GetMessage(string name)
{
ViewBag.strName = name;
return View();
}
public ActionResult CallUs()
{
return View();
}
}
}
کد ویوی CallUs:
@{
ViewBag.Title = "CallUs";
Layout = null;
}
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="~/scripts/jquery.validate.min.js"></script>
<script src="~/scripts/jquery.validate.unobtrusive.min.js"></script>
</head>
<input type="text" name="name" id="name" />
<a class="btn btn-default" href="" id="testLink">تایید</a>
<div id="message"></div>
@Ajax.ActionLink("GetMessage", "GetMessage", "my", new AjaxOptions { UpdateTargetId = "message", InsertionMode = InsertionMode.Replace }, new { @id = "get_message_link" })
<script>
$("#testLink").click(function (e) {
e.preventDefault();
var str = "/my/GetMessage?name=" + $('#name').val();
$("#get_message_link").attr('href', str);
document.getElementById("get_message_link").click( );
});
</script>
کدهای ویوی GetMessage:
<p>welcome @ViewBag.strName</p>