gpt4 book ai didi

javascript - MVC Controller Action 返回 html 而不是 bool

转载 作者:太空宇宙 更新时间:2023-11-03 13:14:07 25 4
gpt4 key购买 nike

我正在尝试从模态调用 Controller 操作并在模态上显示一条消息,说明更新是否成功。

应该很简单吧?不适合我:)

出于某种原因, Controller 操作的结果在浏览器中呈现,而不是返回到我的 jquery,我想用...更新模态

我无法弄清楚为什么“true”或“false”返回到浏览器而不是 jquery?

这是我的 Controller Action :

[HttpPost]
public bool Add(ViewModel model)
{
try
{
// update the achievement
model.achievementItem.EventDate = DateTime.Now;
model.achievementItem.StaffID = CurrentUser.StaffID;
bool success = AddAchievement(GlobalVariables.networkstuff, GlobalVariables.testAuth, model.achievementItem);

return success;
}
catch
{
return false;

}
}

这是我的模式,javasctipt 在底部。

目前我只是尝试将响应放入

    <div d="detailsDiv"></div>

Here is my partial view:

@model Models.ViewModel
<div class="modal-body">
<div>Add achievement for FirstName LastName</div>
<hr />
@using (Html.BeginForm("Add", "Achievement", FormMethod.Post, new { id = "add" }))
{
<div>
<div>Achievement Type (Required)</div>
<div>@Html.DropDownListFor(x => x.achievementItem.BehaviourType, Model.achivementDetails.PositiveOutcomes.Select(d => new SelectListItem { Text = d.Description, Value = d.Code }), new { @class = "form-control" })</div>
<div>Activity Type (Optional)</div>
<div>@Html.DropDownListFor(x => x.achievementItem.Subject, Model.achivementDetails.EventSubjects.Select(d => new SelectListItem { Text = d.Description, Value = d.Code }), "", new { @class = "form-control" })</div>
<div>Awared Given (Optional)</div>
<div>@Html.DropDownListFor(x => x.achievementItem.PositiveOutcome, Model.achivementDetails.PositiveEventTypes.Select(d => new SelectListItem { Text = d.Description, Value = d.Code }), "", new { @class = "form-control" })</div>
<div>@Html.TextAreaFor(x => x.achievementItem.Comment, new { @class = "field span12" })</div>
@Html.HiddenFor(x => x.achievementItem.StudentID, "1")
</div>

<br />
<br />
<div class="row">
<div class="col-md-4 col-md-offset-4">
<button type="button" class="btn btn-default"
data-dismiss="modal">
Cancel
</button>
<button type="submit" id="approve-btn"
class="btn btn-danger">
Save
</button>
</div>
</div>
<div id="detailsDiv"></div>
}
</div>
<script type="text/javascript">
$(function () {
$('#add').submit(function () { // catch the form's submit event
$.ajax({ // create an AJAX call...
data: $(this).serialize(), // get the form data
type: $(this).attr('POST'), // GET or POST
url: $(this).attr('add'), // the file to call
success: function (response) { // on success..
$('#detailsDiv').html(response); // update the DIV
}
});
return false; // cancel original event to prevent form submitting
});
});
</script>

谁能提出为什么 bool 值返回浏览器而不返回 javascript 的原因?

谢谢

更新:

这是我现在根据建议编写的代码,但 json 响应实际上作为要下载的文件在浏览器中弹出,而不是在

Controller Action :

[HttpPost]
public ActionResult Add(ViewModel model)
{
try
{
// update the achievement
model.achievementItem.EventDate = DateTime.Now;
model.achievementItem.StaffID = CurrentUser.StaffID;
bool success = SIMSClient.ClientFunctions.AddAchievement(GlobalVariables.networkstuff, GlobalVariables.testAuth, model.achievementItem);

return Json(success);
}
catch
{
return Json(false);

}
}

部分 View /模态 JavaScript:

<script type="text/javascript">
$(function () {
$('#add').submit(function () { // catch the form's submit event
$.ajax({
data: $(this).serialize(),
type: 'POST',
url: '@Url.Action("Add")',
success: function (response) {
$('#detailsDiv').html(response);
}
});
return false;
});
});

另一个更新:

当我搜索将 json 返回给浏览器的 Controller 时...我发现了这个:

http://www.codeproject.com/Questions/576198/ASP-NETplusMVCplus-plusJSONplusresponseplussendspl

然后我将我的 Controller 操作更改为:

[HttpPost]
public JsonResult Add(ViewModel model)
{
try
{
// update the achievement
model.achievementItem.EventDate = DateTime.Now;
model.achievementItem.StaffID = CurrentUser.StaffID;
bool success = SIMSClient.ClientFunctions.AddAchievement(GlobalVariables.networkstuff, GlobalVariables.testAuth, model.achievementItem);

//return Json(success);
JsonResult result = new JsonResult();
result.Data = success.ToString();
result.ContentType = "text/plain";
return result;

}
catch
{
//return Json(false);
JsonResult result = new JsonResult();
result.Data = "false";
result.ContentType = "text/plain";
return result;
}
}

它仍然显示在浏览器中,而不是部分模态中的 div...

难倒了。

最佳答案

没有'form'的属性POST

尝试如下:

$('#add').submit(function () { // catch the form's submit event
$.ajax({
data: $(this).serialize(),
type: 'POST',
url: '@Url.Action("Add")',
success: function (response) {
$('#detailsDiv').html(response);
}
});
return false;
});

更新:这很简单。看下面的代码

 try
{
// update the achievement
model.achievementItem.EventDate = DateTime.Now;
model.achievementItem.StaffID = CurrentUser.StaffID;
bool success = SIMSClient.ClientFunctions.AddAchievement(GlobalVariables.networkstuff, GlobalVariables.testAuth, model.achievementItem);

if(success){
return Json("true");
}
return Json("false");
}
catch
{
return Json("false");
}

关于javascript - MVC Controller Action 返回 html 而不是 bool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27191979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com