访问对象属性值

时间:2013-05-16 16:19:34

标签: c# asp.net-mvc jsonresult

如何在下面的代码中验证“成功”是真还是假?我尝试使用以下代码,但它无法正常工作:

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

非常感谢任何帮助

2 个答案:

答案 0 :(得分:0)

方法1

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);
}

方法2

$.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');
    }
});

答案 1 :(得分: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) {
    }
});