我正在尝试从模态中调用控制器操作,并在模式上显示一条消息,说明更新是否成功。
应该容易吗?不适合我:)。
由于某种原因,控制器操作的结果是在浏览器中呈现而不是返回到我的jquery,我想用...更新模式...
我无法理解为什么“true”或“false”返回浏览器而不是jquery?
这是我的控制器动作:
[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>
有人可以建议为什么布尔会回到浏览器而不是回到javascript吗?
由于
更新
这是我现在基于建议的代码,但json响应实际上是作为要下载的文件在浏览器中弹出,而不是
控制器操作:
[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);
}
}
部分视图/模态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返回浏览器时...我发现了这个:
http://www.codeproject.com/Questions/576198/ASP-NETplusMVCplus-plusJSONplusresponseplussendspl
然后我将控制器操作更改为:
[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 ...
难住了。
答案 0 :(得分:1)
&#39;表格&#39;
没有属性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");
}
答案 1 :(得分:0)
函数的返回值应该是ActionResult,并且返回的值为Content(success.ToString()):
[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 Content(success.ToString());
}
catch
{
return Content(false.ToString());;
}
}