【发布时间】:2022-06-16 21:13:46
【问题描述】:
我正在尝试将内容从控制器返回到视图,但由于某种原因,它不起作用。我希望“return Contect (....)”中的文本替换标签。 这是我的控制器代码:
namespace Assignment.Controllers
{
public class Q2Controller : Controller
{
// GET: Q2
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult ValidateInput(string myInput)
{
string temp = "";
for (int i = myInput.Length - 1; i >= 0; i--)
{
temp += myInput[i].ToString();
}
if (temp == myInput)
return Content("The string is palindrome");
else
return Content("The string is not palindrome");
}
}
}
这是视图:
<body>
<p>Please enter an alphanumeric string:</p>
<div class="lbl">
@Html.Label("lblpalindrome", "Is it palidrome?")
</div>
<div class="content">
@Html.TextBox("myInput");
<input id="btn1" type="button" value="Enter" onclick="ValidateInput()" />
</div>
</body>
<script>
function ValidateInput() {
var result="";
$.ajax({
url: "@Url.Action("ValidateInput", "Q2")",
type: "POST",
cache: false,
async: true,
data: { myInput: $('#myInput').val() },
success: function (response.data) {
$("#lblpalindrome").text(response.data);
},
error: function (response.data) {
alert("An error occurred: " + response.data);
}
});
}
</script>
【问题讨论】:
-
我建议您使用 Chrome 开发人员工具 (stackoverflow.blog/2022/02/28/…) 或同等工具,以便了解服务器返回给浏览器的确切内容。
标签: jquery ajax asp.net-mvc