Jquery 如何使用AJAX更新文本输入?
我有一个强类型视图,如下所示:Jquery 如何使用AJAX更新文本输入?,jquery,ajax,asp.net-mvc-4,razor,Jquery,Ajax,Asp.net Mvc 4,Razor,我有一个强类型视图,如下所示: @model EmptyWeb.Models.SomeModel @using (@Html.BeginForm(null, null, FormMethod.Post, new { id = "Form1" })) { <fieldset> <div class="editor-field"> @Html.EditorFor(model => model.firstValue)
@model EmptyWeb.Models.SomeModel
@using (@Html.BeginForm(null, null, FormMethod.Post, new { id = "Form1" }))
{
<fieldset>
<div class="editor-field">
@Html.EditorFor(model => model.firstValue)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.secondValue)
</div>
<p>
<input id="Button1" type="button" value="Submit values"/>
</p>
</fieldset>
}
<input id="Text1" type="text" />
$(function () {
$(document).ready(function () {
$("#Button1").click(function () {
$.ajax({
url: "ControllerName/ActionName",
data: $("#Form1").serialize(),
success: function (result) {
$('#Text1').val(result);
},
});
});
});
});
public ActionResult ActionName(string firstValue, string secondValue)
{
if (firstValue > secondValue)
return Content("some string");
else
return Content("some other string");
}
最后是这样的行动:
@model EmptyWeb.Models.SomeModel
@using (@Html.BeginForm(null, null, FormMethod.Post, new { id = "Form1" }))
{
<fieldset>
<div class="editor-field">
@Html.EditorFor(model => model.firstValue)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.secondValue)
</div>
<p>
<input id="Button1" type="button" value="Submit values"/>
</p>
</fieldset>
}
<input id="Text1" type="text" />
$(function () {
$(document).ready(function () {
$("#Button1").click(function () {
$.ajax({
url: "ControllerName/ActionName",
data: $("#Form1").serialize(),
success: function (result) {
$('#Text1').val(result);
},
});
});
});
});
public ActionResult ActionName(string firstValue, string secondValue)
{
if (firstValue > secondValue)
return Content("some string");
else
return Content("some other string");
}
我在这里试图做的是将数据从表单发送到控制器,用这些数据做一些事情,从控制器返回字符串,并将这个字符串写入表单外部的输入。当然,在不刷新页面的情况下完成所有这些,这就是我使用AJAX的原因。尝试这样做会导致加载资源失败:服务器响应状态为404(未找到)。不幸的是,我自己找不到解决这个问题的办法。如果这是重复的问题,我很抱歉。所以我的问题是:我做错了什么?我该怎么做?我必须使用局部视图吗?1)您需要在ajax调用中添加类型:
$(function () {
$(document).ready(function () {
$("#Button1").click(function () {
$.ajax({
url: "ControllerName/ActionName",
data: $("#Form1").serialize(),
type:'POST'
success: function (result) {
$('#Text1').val(result);
},
});
});
});
});
2) 将属性httppost添加到控制器中,并将字符串更改为作为操作参数的模型名称
[HttpPost]
public string ActionName(SomeModel model)
{
if (model.firstValue > model.secondValue)
return "some string";
else
return "some other string";
}
我用你所有的代码创建了一个测试应用程序,但将URL替换为我自己的方法,效果非常好。因此,我认为ajax代码的URL是不正确的。仔细检查您正在使用的url,确保其正确无误。此外,我还为正在比较的字符串添加了.length 注意:如果您的ajax在您的视图中,那么呈现正确路径的简单方法可以执行以下操作
url: "@Url.Action("ActionName")",
以下是我的观点:
@model MvcApplication1.Models.SomeModel
@using (@Html.BeginForm(null, null, FormMethod.Post, new { id = "Form1" }))
{
<fieldset>
<div class="editor-field">
@Html.EditorFor(model => model.firstValue)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.secondValue)
</div>
<p>
<input id="Button1" type="button" value="Submit values"/>
</p>
</fieldset>
}
<input id="Text1" type="text" />
<script>
$(function() {
$(document).ready(function() {
$("#Button1").click(function() {
$.ajax({
url: "Home/ActionName",
data: $("#Form1").serialize(),
success: function(result) {
$('#Text1').val(result);
},
});
});
});
});
</script>
你是否在你的动作中设置了一个断点以查看你的动作是否被击中?是的,我设置了断点,但它没有被击中。