MVC 控制器操作返回 html 而不是布尔值

本文关键字:布尔值 html 控制器 操作 返回 MVC | 更新日期: 2023-09-27 18:32:19

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

应该很容易吧?不适合我:)

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

我无法弄清楚为什么"真"或"假"返回到浏览器而不是 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...

难倒。

MVC 控制器操作返回 html 而不是布尔值

没有"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");
    }

函数的返回值应为 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());;
    }
}