0

如何在下面的代码中验证“成功”是真还是假?我已尝试使用以下代码,但它不起作用:

if (result["success"].Equals(false)) throw new Exception(result["message"].ToString());

if (result["message"].ToString().Contains("maximum limit reached")) throw new Exception(result["message"].ToString());

这是我的行动:

[HttpPost]
    public ActionResult PostFile(string NewFileName, string FileNumber)
    {

        try
        {
            var result = ((JsonResult)(SaveFile(NewFileName, FileNumber))).ToDictionary();
            if (result.Keys.Contains("message")) throw new Exception(result["message"].ToString());
            //if (result["success"].Equals(false)) throw new Exception(result["message"].ToString());

            return Content("success");
        }
        catch (Exception ex)
        {
            return Content(ex.ToString());
        }

    }

    public ActionResult SaveFile(string status, string FileNumber)
    {

        try
        {
            var currentPath = ConfigurationManager.AppSettings["FilePath"]; 
            string filename = FileNumber + ".pdf";
            var ext = UploadHandler.SaveUploadedFile(Path.GetDirectoryName(currentPath), filename);
            return Json(new { success = true }, "text/html"); 
        }
        catch (Exception ex)
        {
            return Json(new { success = false, message = ex.Message }, "text/html");
        }
    }

任何帮助深表感谢

4

2 回答 2

0

方法一

private T GetValueFromJsonResult<T>(JsonResult jsonResult, string propertyName)
{
    var property =
        jsonResult.Data.GetType().GetProperties()
        .Where(p => string.Compare(p.Name, propertyName) == 0)
        .FirstOrDefault();

    if (null == property)
        throw new ArgumentException(propertyName + "not found", propertyName);
    return (T)property.GetValue(jsonResult.Data, null);
}

Boolean testValue = GetValueFromJsonResult<bool>(abc(), "Success");

private JsonResult abc()
{
    return Json(new { Success = true }, JsonRequestBehavior.AllowGet);
}

方法二

$.ajax({
    url: 'ControllerName/PostFile',
    data: JSON.stringify({ NewFileName: "FileName", FileNumber: "12" }),
    type: 'POST',
    contentType: 'application/json, charset=utf-8',
    dataType: 'json',
    beforeSend: function (xhr, opts) {
    }
}).done(function (data) {
    if(data.success) {
        alert('ok');
    }
});
于 2013-05-16T16:30:06.723 回答
0

您可以分离出您的逻辑并使用方法组合Request.IsAjaxRequest()方法。

private void UploadFile(string status, string fileNumber)
{
    var currentPath = ConfigurationManager.AppSettings["FilePath"]; 
    string filename = fileNumber + ".pdf";
    UploadHandler.SaveUploadedFile(Path.GetDirectoryName(currentPath), filename);
}

[HttpPost]
public ActionResult PostFile(string newFileName, string fileNumber)
{
    var isAjax = Request.IsAjaxRequest();

    try
    {
        UploadFile(newFileName, fileNumber);
    }
    catch (Exception ex)
    {
        if(isAjax)
        {
            return Json(new { success = false, message = ex.Message }, "text/html");
        }

        return Content(ex.ToString());
    }

    if(isAjax)
    {
        return Json(new { success = true }, "text/html"); 
    }

    return Content("success");
}

从 Javascript 调用:

$.ajax({
    type: 'POST',
    url: 'Controller/PostFile',
    data: { newFileName = '', fileNumber = '' },
    success: function(content) {
    }
});
于 2013-05-16T16:55:02.960 回答